From 8f179dc508736ed25b16e84d2528c225d0ba865c Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Fri, 22 Mar 2013 17:14:41 -0700 Subject: nexuiz: Replace individual shaders with shader_test files. --- shaders/nexuiz/1.frag | 1015 ------------- shaders/nexuiz/1.vert | 1015 ------------- shaders/nexuiz/10.frag | 1016 ------------- shaders/nexuiz/10.shader_test | 3158 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/10.vert | 1016 ------------- shaders/nexuiz/100.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/103.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/106.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/109.shader_test | 3160 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/11.frag | 1017 ------------- shaders/nexuiz/11.vert | 1017 ------------- shaders/nexuiz/112.shader_test | 3156 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/115.shader_test | 3160 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/118.shader_test | 3225 ++++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/12.frag | 1016 ------------- shaders/nexuiz/12.vert | 1016 ------------- shaders/nexuiz/121.shader_test | 3170 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/13.frag | 1016 ------------- shaders/nexuiz/13.shader_test | 3170 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/13.vert | 1016 ------------- shaders/nexuiz/14.frag | 1016 ------------- shaders/nexuiz/14.vert | 1016 ------------- shaders/nexuiz/16.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/19.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/2.frag | 1015 ------------- shaders/nexuiz/2.vert | 1015 ------------- shaders/nexuiz/22.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/25.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/28.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/3.frag | 1015 ------------- shaders/nexuiz/3.shader_test | 17 + shaders/nexuiz/3.vert | 1015 ------------- shaders/nexuiz/31.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/34.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/37.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/4.frag | 1015 ------------- shaders/nexuiz/4.vert | 1015 ------------- shaders/nexuiz/40.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/43.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/46.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/49.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/5.frag | 1016 ------------- shaders/nexuiz/5.vert | 1016 ------------- shaders/nexuiz/52.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/55.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/58.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/6.frag | 1016 ------------- shaders/nexuiz/6.shader_test | 22 + shaders/nexuiz/6.vert | 1016 ------------- shaders/nexuiz/61.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/64.shader_test | 3156 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/67.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/7.frag | 1017 ------------- shaders/nexuiz/7.shader_test | 3158 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/7.vert | 1017 ------------- shaders/nexuiz/70.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/73.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/76.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/79.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/8.frag | 1015 ------------- shaders/nexuiz/8.vert | 1015 ------------- shaders/nexuiz/82.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/85.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/88.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/9.frag | 1016 ------------- shaders/nexuiz/9.vert | 1016 ------------- shaders/nexuiz/91.shader_test | 3157 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/94.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ shaders/nexuiz/97.shader_test | 3155 +++++++++++++++++++++++++++++++++++++++ 69 files changed, 123204 insertions(+), 28442 deletions(-) delete mode 100644 shaders/nexuiz/1.frag delete mode 100644 shaders/nexuiz/1.vert delete mode 100644 shaders/nexuiz/10.frag create mode 100644 shaders/nexuiz/10.shader_test delete mode 100644 shaders/nexuiz/10.vert create mode 100644 shaders/nexuiz/100.shader_test create mode 100644 shaders/nexuiz/103.shader_test create mode 100644 shaders/nexuiz/106.shader_test create mode 100644 shaders/nexuiz/109.shader_test delete mode 100644 shaders/nexuiz/11.frag delete mode 100644 shaders/nexuiz/11.vert create mode 100644 shaders/nexuiz/112.shader_test create mode 100644 shaders/nexuiz/115.shader_test create mode 100644 shaders/nexuiz/118.shader_test delete mode 100644 shaders/nexuiz/12.frag delete mode 100644 shaders/nexuiz/12.vert create mode 100644 shaders/nexuiz/121.shader_test delete mode 100644 shaders/nexuiz/13.frag create mode 100644 shaders/nexuiz/13.shader_test delete mode 100644 shaders/nexuiz/13.vert delete mode 100644 shaders/nexuiz/14.frag delete mode 100644 shaders/nexuiz/14.vert create mode 100644 shaders/nexuiz/16.shader_test create mode 100644 shaders/nexuiz/19.shader_test delete mode 100644 shaders/nexuiz/2.frag delete mode 100644 shaders/nexuiz/2.vert create mode 100644 shaders/nexuiz/22.shader_test create mode 100644 shaders/nexuiz/25.shader_test create mode 100644 shaders/nexuiz/28.shader_test delete mode 100644 shaders/nexuiz/3.frag create mode 100644 shaders/nexuiz/3.shader_test delete mode 100644 shaders/nexuiz/3.vert create mode 100644 shaders/nexuiz/31.shader_test create mode 100644 shaders/nexuiz/34.shader_test create mode 100644 shaders/nexuiz/37.shader_test delete mode 100644 shaders/nexuiz/4.frag delete mode 100644 shaders/nexuiz/4.vert create mode 100644 shaders/nexuiz/40.shader_test create mode 100644 shaders/nexuiz/43.shader_test create mode 100644 shaders/nexuiz/46.shader_test create mode 100644 shaders/nexuiz/49.shader_test delete mode 100644 shaders/nexuiz/5.frag delete mode 100644 shaders/nexuiz/5.vert create mode 100644 shaders/nexuiz/52.shader_test create mode 100644 shaders/nexuiz/55.shader_test create mode 100644 shaders/nexuiz/58.shader_test delete mode 100644 shaders/nexuiz/6.frag create mode 100644 shaders/nexuiz/6.shader_test delete mode 100644 shaders/nexuiz/6.vert create mode 100644 shaders/nexuiz/61.shader_test create mode 100644 shaders/nexuiz/64.shader_test create mode 100644 shaders/nexuiz/67.shader_test delete mode 100644 shaders/nexuiz/7.frag create mode 100644 shaders/nexuiz/7.shader_test delete mode 100644 shaders/nexuiz/7.vert create mode 100644 shaders/nexuiz/70.shader_test create mode 100644 shaders/nexuiz/73.shader_test create mode 100644 shaders/nexuiz/76.shader_test create mode 100644 shaders/nexuiz/79.shader_test delete mode 100644 shaders/nexuiz/8.frag delete mode 100644 shaders/nexuiz/8.vert create mode 100644 shaders/nexuiz/82.shader_test create mode 100644 shaders/nexuiz/85.shader_test create mode 100644 shaders/nexuiz/88.shader_test delete mode 100644 shaders/nexuiz/9.frag delete mode 100644 shaders/nexuiz/9.vert create mode 100644 shaders/nexuiz/91.shader_test create mode 100644 shaders/nexuiz/94.shader_test create mode 100644 shaders/nexuiz/97.shader_test (limited to 'shaders') diff --git a/shaders/nexuiz/1.frag b/shaders/nexuiz/1.frag deleted file mode 100644 index 9c4b6ac..0000000 --- a/shaders/nexuiz/1.frag +++ /dev/null @@ -1,1015 +0,0 @@ -#define FRAGMENT_SHADER -#define MODE_GENERIC -#define USEDIFFUSE - - - - - - - - - - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/1.vert b/shaders/nexuiz/1.vert deleted file mode 100644 index 893022b..0000000 --- a/shaders/nexuiz/1.vert +++ /dev/null @@ -1,1015 +0,0 @@ -#define VERTEX_SHADER -#define MODE_GENERIC -#define USEDIFFUSE - - - - - - - - - - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/10.frag b/shaders/nexuiz/10.frag deleted file mode 100644 index 271a631..0000000 --- a/shaders/nexuiz/10.frag +++ /dev/null @@ -1,1016 +0,0 @@ -#define FRAGMENT_SHADER -#define MODE_POSTPROCESS - - - - - -#define USEGLOW -#define USEBLOOM - - - - - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/10.shader_test b/shaders/nexuiz/10.shader_test new file mode 100644 index 0000000..8c8beda --- /dev/null +++ b/shaders/nexuiz/10.shader_test @@ -0,0 +1,3158 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_GENERIC +#define USEDIFFUSE + +#define USEVIEWTINT +#define USECOLORMAPPING + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // RemovePlaying demo demos/demo1.dem. +Host_Mingled: time stepped forward (went from 0.000000 to 1363996717.188938, difference 1363996717.188938) +<-- server to client keepalive + this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_GENERIC +#define USEDIFFUSE + +#define USEVIEWTINT +#define USECOLORMAPPING + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/10.vert b/shaders/nexuiz/10.vert deleted file mode 100644 index e0f12fc..0000000 --- a/shaders/nexuiz/10.vert +++ /dev/null @@ -1,1016 +0,0 @@ -#define VERTEX_SHADER -#define MODE_POSTPROCESS - - - - - -#define USEGLOW -#define USEBLOOM - - - - - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/100.shader_test b/shaders/nexuiz/100.shader_test new file mode 100644 index 0000000..275d4d2 --- /dev/null +++ b/shaders/nexuiz/100.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_FLATCOLOR + + + +#define USECOLORMAPPING + + + + + + + +#define USEGLOW + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_FLATCOLOR + + + +#define USECOLORMAPPING + + + + + + + +#define USEGLOW + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/103.shader_test b/shaders/nexuiz/103.shader_test new file mode 100644 index 0000000..5f39637 --- /dev/null +++ b/shaders/nexuiz/103.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTDIRECTION +#define USEDIFFUSE + + +#define USECOLORMAPPING + + + + + + + + + + + + + + + + + + +#define USEALPHAKILL + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTDIRECTION +#define USEDIFFUSE + + +#define USECOLORMAPPING + + + + + + + + + + + + + + + + + + +#define USEALPHAKILL + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/106.shader_test b/shaders/nexuiz/106.shader_test new file mode 100644 index 0000000..2ba0caa --- /dev/null +++ b/shaders/nexuiz/106.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTSOURCE +#define USEDIFFUSE + + +#define USECOLORMAPPING + + + + + + + + + + + + +#define USEOFFSETMAPPING + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTSOURCE +#define USEDIFFUSE + + +#define USECOLORMAPPING + + + + + + + + + + + + +#define USEOFFSETMAPPING + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/109.shader_test b/shaders/nexuiz/109.shader_test new file mode 100644 index 0000000..d08c503 --- /dev/null +++ b/shaders/nexuiz/109.shader_test @@ -0,0 +1,3160 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTSOURCE +#define USEDIFFUSE + + +#define USECOLORMAPPING + + + + + + + + + + + + + + + + + + +#define USEALPHAKILL + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf[BOT]Roadkill was gunned by [BOT]Death +[BOT]Airhead was gunned by [BOT]Deadline +[BOT]Newbie was gunned by NexPlayer +[BOT]Lion was gunned by NexPlayer +You got the Electro +3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTSOURCE +#define USEDIFFUSE + + +#define USECOLORMAPPING + + + + + + + + + + + + + + + + + + +#define USEALPHAKILL + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/11.frag b/shaders/nexuiz/11.frag deleted file mode 100644 index 685cd1a..0000000 --- a/shaders/nexuiz/11.frag +++ /dev/null @@ -1,1017 +0,0 @@ -#define FRAGMENT_SHADER -#define MODE_LIGHTDIRECTION -#define USEDIFFUSE - -#define USECOLORMAPPING -#define USESATURATION - - -#define USEGLOW -#define USEBLOOM - - - -#define USEOFFSETMAPPING - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/11.vert b/shaders/nexuiz/11.vert deleted file mode 100644 index 7c6417d..0000000 --- a/shaders/nexuiz/11.vert +++ /dev/null @@ -1,1017 +0,0 @@ -#define VERTEX_SHADER -#define MODE_LIGHTDIRECTION -#define USEDIFFUSE - -#define USECOLORMAPPING -#define USESATURATION - - -#define USEGLOW -#define USEBLOOM - - - -#define USEOFFSETMAPPING - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/112.shader_test b/shaders/nexuiz/112.shader_test new file mode 100644 index 0000000..927cb0f --- /dev/null +++ b/shaders/nexuiz/112.shader_test @@ -0,0 +1,3156 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_POSTPROCESS + + +#define USEVIEWTINT + + + + + + +#define USEGAMMARAMPS + + +#define USEBLOOM + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegri[BOT]Resurrection was gunned by [BOT]Deadline +d_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_POSTPROCESS + + +#define USEVIEWTINT + + + + + + +#define USEGAMMARAMPS + + +#define USEBLOOM + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/115.shader_test b/shaders/nexuiz/115.shader_test new file mode 100644 index 0000000..2cab6ca --- /dev/null +++ b/shaders/nexuiz/115.shader_test @@ -0,0 +1,3160 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTSOURCE +#define USEDIFFUSE + + +#define USECOLORMAPPING + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // reflection must come last because it already contains exactly the correct fog (the reflection render preserves ca[BOT]Deadline was gunned by [BOT]Newbie +[BOT]Death was gunned by [BOT]Roadkill +[BOT]Darkness was blasted by NexPlayer +NexPlayer has 3 frags in a row +NexPlayer made a TRIPLE FRAG +mera distance from the plane, it only flips the side) and ContrastBoost/SceneBrightness +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTSOURCE +#define USEDIFFUSE + + +#define USECOLORMAPPING + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/118.shader_test b/shaders/nexuiz/118.shader_test new file mode 100644 index 0000000..59c3875 --- /dev/null +++ b/shaders/nexuiz/118.shader_test @@ -0,0 +1,3225 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTDIRECTION +#define USEDIFFUSE + + + + + + + + + + + + +#define USESPECULAR + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4[BOT]Roadkill was gunned by [BOT]Airhead +[BOT]Spellbinder was gunned by [BOT]Newbie +You got the Crylink +[BOT]Deadline was blasted by [BOT]Death +[BOT]Newbie was blasted by NexPlayer +NexPlayer has 4 frags in a row +[BOT]Spellbinder was blasted by NexPlayer +NexPlayer has 5 frags in a row +NexPlayer made a FIVE FRAG COMBO +[BOT]Roadkill was gunned by [BOT]Airhead +[BOT]Death exploded +NexPlayer played with plasma +NexPlayer ended it all with a 5 kill spree +[BOT]Airhead was blasted by NexPlayer +[BOT]Darkness exploded +[BOT]Death was blasted by [BOT]Darkness +[BOT]Resurrection was gunned by [BOT]Deadline +[BOT]Spellbinder was blasted by [BOT]Lion +[BOT]Lion exploded +[BOT]Airhead has been vaporized by [BOT]Deadline +[BOT]Deadline was blasted by [BOT]Roadkill +[BOT]Roadkill was blasted by [BOT]Newbie +You got the Rocket Launcher +[BOT]Newbie was gunned by [BOT]Death +[BOT]Death was blasted by NexPlayer +[BOT]Resurrection was gunned by [BOT]Newbie +[BOT]Deadline was gunned by [BOT]Roadkill +You got the Electro +You got the Mortar +[BOT]Darkness was blasted by NexPlayer +NexPlayer has 3 frags in a row +NexPlayer made a TRIPLE FRAG +[BOT]Airhead has been vaporized by [BOT]Death +[BOT]Spellbinder has been vaporized by [BOT]Lion +[BOT]Newbie has been vaporized by [BOT]Lion +[BOT]Resurrection was blasted by NexPlayer +NexPlayer has 4 frags in a row +[BOT]Lion was blasted by NexPlayer +NexPlayer has 5 frags in a row +NexPlayer made a FIVE FRAG COMBO +You got the Shotgun +[BOT]Airhead was blasted by [BOT]Roadkill +[BOT]Deadline was blasted by NexPlayer +NexPlayer has 6 frags in a row +[BOT]Death exploded +[BOT]Roadkill was blasted by NexPlayer +NexPlayer has 7 frags in a row +[BOT]Newbie was blasted by [BOT]Darkness +[BOT]Darkness exploded +You got the Shotgun +[BOT]Death exploded +[BOT]Darkness was blasted by [BOT]Spellbinder +NexPlayer was gunned by [BOT]Lion +NexPlayer's 7 kill spree was ended by [BOT]Lion +[BOT]Lion was riddled full of holes by [BOT]Deadline +[BOT]Roadkill played with plasma +[BOT]Resurrection was blasted by [BOT]Newbie +[BOT]Newbie detonated +[BOT]Deadline was blasted by [BOT]Newbie +[BOT]Airhead was gunned by [BOT]Death +[BOT]Spellbinder played with plasma +[BOT]Death was gunned by NexPlayer +You got the Shotgun +[BOT]Darkness was gunned by [BOT]Airhead +[BOT]Lion exploded +[BOT]Roadkill couldn't resist the urge to self-destruct +[BOT]Airhead was blasted by [BOT]Newbie +You got the Crylink +[BOT]Death was riddled full of holes by [BOT]Darkness +[BOT]Spellbinder was blasted by NexPlayer + ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTDIRECTION +#define USEDIFFUSE + + + + + + + + + + + + +#define USESPECULAR + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/12.frag b/shaders/nexuiz/12.frag deleted file mode 100644 index d31aec2..0000000 --- a/shaders/nexuiz/12.frag +++ /dev/null @@ -1,1016 +0,0 @@ -#define FRAGMENT_SHADER -#define MODE_LIGHTDIRECTION -#define USEDIFFUSE - - - - -#define USEGLOW -#define USEBLOOM -#define USESPECULAR -#define USEPOSTPROCESSING - -#define USEOFFSETMAPPING - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/12.vert b/shaders/nexuiz/12.vert deleted file mode 100644 index 9f72bb9..0000000 --- a/shaders/nexuiz/12.vert +++ /dev/null @@ -1,1016 +0,0 @@ -#define VERTEX_SHADER -#define MODE_LIGHTDIRECTION -#define USEDIFFUSE - - - - -#define USEGLOW -#define USEBLOOM -#define USESPECULAR -#define USEPOSTPROCESSING - -#define USEOFFSETMAPPING - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/121.shader_test b/shaders/nexuiz/121.shader_test new file mode 100644 index 0000000..aa8973e --- /dev/null +++ b/shaders/nexuiz/121.shader_test @@ -0,0 +1,3170 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_VERTEXCOLOR + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefraYou got the Rocket Launcher +[BOT]Darkness was gunned by [BOT]Resurrection +[BOT]Airhead was blasted by [BOT]Deadline +[BOT]Resurrection was blasted by [BOT]Deadline +[BOT]Deadline was blasted by NexPlayer +NexPlayer has 3 frags in a row +NexPlayer made a TRIPLE FRAG +[BOT]Roadkill was blasted by NexPlayer +NexPlayer has 4 frags in a row +[BOT]Newbie detonated +NexPlayer exploded +NexPlayer ended it all with a 4 kill spree +[BOT]Lion was gunned by [BOT]Death +Server disconnected +1910 frames 34.9796970 seconds 54.6031031 fps, one-second fps min/avg/max: 33 56 81 (90 seconds) +ctReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_VERTEXCOLOR + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/13.frag b/shaders/nexuiz/13.frag deleted file mode 100644 index b325893..0000000 --- a/shaders/nexuiz/13.frag +++ /dev/null @@ -1,1016 +0,0 @@ -#define FRAGMENT_SHADER -#define MODE_LIGHTDIRECTION -#define USEDIFFUSE - - - - -#define USEGLOW -#define USEBLOOM - - - -#define USEOFFSETMAPPING - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/13.shader_test b/shaders/nexuiz/13.shader_test new file mode 100644 index 0000000..c7124b7 --- /dev/null +++ b/shaders/nexuiz/13.shader_test @@ -0,0 +1,3170 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_GENERIC + + +#define USEVIEWTINT +#define USECOLORMAPPING + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractRefle +Server: Nexuiz build 04:31:17 Jun 8 2006 (progs 36282 crc)Serverinfo packet received. + +<===================================> + +Stabilized Warfare: Resurrection - by Tymo +Sound sound/plats/medplat1.wav not found +Sound sound/plats/medplat2.wav not found +Sound sound/weapons/electro_fly.wav not found +Sound sound/weapons/rocket_fly.wav not found +Sound sound/sound/ambient/machinerydrone01.wav not found +Sound sound/sound/ambient/fan4.wav not found +Sound sound/sound/ambient/demonwind01.wav not found +couldn't exec maps/warfare.cfg +<-- server to client keepalive +ct.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_GENERIC + + +#define USEVIEWTINT +#define USECOLORMAPPING + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/13.vert b/shaders/nexuiz/13.vert deleted file mode 100644 index 5f7fe9b..0000000 --- a/shaders/nexuiz/13.vert +++ /dev/null @@ -1,1016 +0,0 @@ -#define VERTEX_SHADER -#define MODE_LIGHTDIRECTION -#define USEDIFFUSE - - - - -#define USEGLOW -#define USEBLOOM - - - -#define USEOFFSETMAPPING - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/14.frag b/shaders/nexuiz/14.frag deleted file mode 100644 index f53a006..0000000 --- a/shaders/nexuiz/14.frag +++ /dev/null @@ -1,1016 +0,0 @@ -#define FRAGMENT_SHADER -#define MODE_LIGHTDIRECTION -#define USEDIFFUSE - -#define USECOLORMAPPING -#define USESATURATION - - - -#define USESPECULAR -#define USEPOSTPROCESSING - -#define USEOFFSETMAPPING - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/14.vert b/shaders/nexuiz/14.vert deleted file mode 100644 index e1a7303..0000000 --- a/shaders/nexuiz/14.vert +++ /dev/null @@ -1,1016 +0,0 @@ -#define VERTEX_SHADER -#define MODE_LIGHTDIRECTION -#define USEDIFFUSE - -#define USECOLORMAPPING -#define USESATURATION - - - -#define USESPECULAR -#define USEPOSTPROCESSING - -#define USEOFFSETMAPPING - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/16.shader_test b/shaders/nexuiz/16.shader_test new file mode 100644 index 0000000..b04ddf8 --- /dev/null +++ b/shaders/nexuiz/16.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTDIRECTIONMAP_MODELSPACE +#define USEDIFFUSE + + + + + + + + + + + + +#define USESPECULAR + + +#define USEOFFSETMAPPING + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTDIRECTIONMAP_MODELSPACE +#define USEDIFFUSE + + + + + + + + + + + + +#define USESPECULAR + + +#define USEOFFSETMAPPING + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/19.shader_test b/shaders/nexuiz/19.shader_test new file mode 100644 index 0000000..a193f84 --- /dev/null +++ b/shaders/nexuiz/19.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTDIRECTIONMAP_MODELSPACE +#define USEDIFFUSE + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTDIRECTIONMAP_MODELSPACE +#define USEDIFFUSE + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/2.frag b/shaders/nexuiz/2.frag deleted file mode 100644 index f59ae35..0000000 --- a/shaders/nexuiz/2.frag +++ /dev/null @@ -1,1015 +0,0 @@ -#define FRAGMENT_SHADER -#define MODE_GENERIC - - - - - - - - - - - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/2.vert b/shaders/nexuiz/2.vert deleted file mode 100644 index e58a84b..0000000 --- a/shaders/nexuiz/2.vert +++ /dev/null @@ -1,1015 +0,0 @@ -#define VERTEX_SHADER -#define MODE_GENERIC - - - - - - - - - - - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/22.shader_test b/shaders/nexuiz/22.shader_test new file mode 100644 index 0000000..8e4f94e --- /dev/null +++ b/shaders/nexuiz/22.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTDIRECTIONMAP_MODELSPACE +#define USEDIFFUSE + + + + + + + + + + + + + + + +#define USEOFFSETMAPPING + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTDIRECTIONMAP_MODELSPACE +#define USEDIFFUSE + + + + + + + + + + + + + + + +#define USEOFFSETMAPPING + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/25.shader_test b/shaders/nexuiz/25.shader_test new file mode 100644 index 0000000..700f798 --- /dev/null +++ b/shaders/nexuiz/25.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTDIRECTION +#define USEDIFFUSE + + +#define USECOLORMAPPING + + + + + + + +#define USEGLOW + +#define USESPECULAR + + +#define USEOFFSETMAPPING + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTDIRECTION +#define USEDIFFUSE + + +#define USECOLORMAPPING + + + + + + + +#define USEGLOW + +#define USESPECULAR + + +#define USEOFFSETMAPPING + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/28.shader_test b/shaders/nexuiz/28.shader_test new file mode 100644 index 0000000..a856b6c --- /dev/null +++ b/shaders/nexuiz/28.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTDIRECTION +#define USEDIFFUSE + + +#define USECOLORMAPPING + + + + + + + +#define USEGLOW + +#define USESPECULAR + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTDIRECTION +#define USEDIFFUSE + + +#define USECOLORMAPPING + + + + + + + +#define USEGLOW + +#define USESPECULAR + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/3.frag b/shaders/nexuiz/3.frag deleted file mode 100644 index 0529ca5..0000000 --- a/shaders/nexuiz/3.frag +++ /dev/null @@ -1,1015 +0,0 @@ -#define FRAGMENT_SHADER -#define MODE_LIGHTDIRECTIONMAP_MODELSPACE -#define USEDIFFUSE - - - - - -#define USESPECULAR -#define USEPOSTPROCESSING - -#define USEOFFSETMAPPING - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/3.shader_test b/shaders/nexuiz/3.shader_test new file mode 100644 index 0000000..30ba541 --- /dev/null +++ b/shaders/nexuiz/3.shader_test @@ -0,0 +1,17 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +uniform vec4 color; +void main() +{ + gl_FragColor = color; +} + +[vertex shader] +attribute vec4 position; +void main() +{ + gl_Position = position; +} + diff --git a/shaders/nexuiz/3.vert b/shaders/nexuiz/3.vert deleted file mode 100644 index 646c464..0000000 --- a/shaders/nexuiz/3.vert +++ /dev/null @@ -1,1015 +0,0 @@ -#define VERTEX_SHADER -#define MODE_LIGHTDIRECTIONMAP_MODELSPACE -#define USEDIFFUSE - - - - - -#define USESPECULAR -#define USEPOSTPROCESSING - -#define USEOFFSETMAPPING - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/31.shader_test b/shaders/nexuiz/31.shader_test new file mode 100644 index 0000000..b3c8687 --- /dev/null +++ b/shaders/nexuiz/31.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_FLATCOLOR + + + +#define USECOLORMAPPING + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_FLATCOLOR + + + +#define USECOLORMAPPING + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/34.shader_test b/shaders/nexuiz/34.shader_test new file mode 100644 index 0000000..f012ab2 --- /dev/null +++ b/shaders/nexuiz/34.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_GENERIC +#define USEDIFFUSE + +#define USEVIEWTINT +#define USECOLORMAPPING + + + + + + + + + + + + + + + + + + + +#define USEREFLECTCUBE + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_GENERIC +#define USEDIFFUSE + +#define USEVIEWTINT +#define USECOLORMAPPING + + + + + + + + + + + + + + + + + + + +#define USEREFLECTCUBE + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/37.shader_test b/shaders/nexuiz/37.shader_test new file mode 100644 index 0000000..4c8c076 --- /dev/null +++ b/shaders/nexuiz/37.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_POSTPROCESS + + + + + + + + + +#define USEGAMMARAMPS + + +#define USEBLOOM + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_POSTPROCESS + + + + + + + + + +#define USEGAMMARAMPS + + +#define USEBLOOM + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/4.frag b/shaders/nexuiz/4.frag deleted file mode 100644 index 85f3c2b..0000000 --- a/shaders/nexuiz/4.frag +++ /dev/null @@ -1,1015 +0,0 @@ -#define FRAGMENT_SHADER -#define MODE_LIGHTDIRECTIONMAP_MODELSPACE -#define USEDIFFUSE - - - - - - - - -#define USEOFFSETMAPPING - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/4.vert b/shaders/nexuiz/4.vert deleted file mode 100644 index b4af4a5..0000000 --- a/shaders/nexuiz/4.vert +++ /dev/null @@ -1,1015 +0,0 @@ -#define VERTEX_SHADER -#define MODE_LIGHTDIRECTIONMAP_MODELSPACE -#define USEDIFFUSE - - - - - - - - -#define USEOFFSETMAPPING - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/40.shader_test b/shaders/nexuiz/40.shader_test new file mode 100644 index 0000000..7eab129 --- /dev/null +++ b/shaders/nexuiz/40.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_FLATCOLOR + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_FLATCOLOR + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/43.shader_test b/shaders/nexuiz/43.shader_test new file mode 100644 index 0000000..7fb8116 --- /dev/null +++ b/shaders/nexuiz/43.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTDIRECTIONMAP_MODELSPACE +#define USEDIFFUSE + + + + + + + + + + +#define USEGLOW + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTDIRECTIONMAP_MODELSPACE +#define USEDIFFUSE + + + + + + + + + + +#define USEGLOW + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/46.shader_test b/shaders/nexuiz/46.shader_test new file mode 100644 index 0000000..dfb898f --- /dev/null +++ b/shaders/nexuiz/46.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTDIRECTIONMAP_MODELSPACE +#define USEDIFFUSE + + + + + + + + + + +#define USEGLOW + +#define USESPECULAR + + +#define USEOFFSETMAPPING + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTDIRECTIONMAP_MODELSPACE +#define USEDIFFUSE + + + + + + + + + + +#define USEGLOW + +#define USESPECULAR + + +#define USEOFFSETMAPPING + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/49.shader_test b/shaders/nexuiz/49.shader_test new file mode 100644 index 0000000..013cd3c --- /dev/null +++ b/shaders/nexuiz/49.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTDIRECTIONMAP_MODELSPACE +#define USEDIFFUSE + + + + + + + + + + + + +#define USESPECULAR + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTDIRECTIONMAP_MODELSPACE +#define USEDIFFUSE + + + + + + + + + + + + +#define USESPECULAR + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/5.frag b/shaders/nexuiz/5.frag deleted file mode 100644 index 14ee1fa..0000000 --- a/shaders/nexuiz/5.frag +++ /dev/null @@ -1,1016 +0,0 @@ -#define FRAGMENT_SHADER -#define MODE_LIGHTDIRECTIONMAP_MODELSPACE -#define USEDIFFUSE - - - - -#define USEGLOW -#define USEBLOOM -#define USESPECULAR -#define USEPOSTPROCESSING - -#define USEOFFSETMAPPING - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/5.vert b/shaders/nexuiz/5.vert deleted file mode 100644 index 83f630e..0000000 --- a/shaders/nexuiz/5.vert +++ /dev/null @@ -1,1016 +0,0 @@ -#define VERTEX_SHADER -#define MODE_LIGHTDIRECTIONMAP_MODELSPACE -#define USEDIFFUSE - - - - -#define USEGLOW -#define USEBLOOM -#define USESPECULAR -#define USEPOSTPROCESSING - -#define USEOFFSETMAPPING - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/52.shader_test b/shaders/nexuiz/52.shader_test new file mode 100644 index 0000000..298bb23 --- /dev/null +++ b/shaders/nexuiz/52.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTDIRECTIONMAP_MODELSPACE +#define USEDIFFUSE + + + + + + + + + + + + + + + + + + + + + +#define USEALPHAKILL + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTDIRECTIONMAP_MODELSPACE +#define USEDIFFUSE + + + + + + + + + + + + + + + + + + + + + +#define USEALPHAKILL + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/55.shader_test b/shaders/nexuiz/55.shader_test new file mode 100644 index 0000000..e365332 --- /dev/null +++ b/shaders/nexuiz/55.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTDIRECTION +#define USEDIFFUSE + + +#define USECOLORMAPPING + + + + + + + +#define USEGLOW + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTDIRECTION +#define USEDIFFUSE + + +#define USECOLORMAPPING + + + + + + + +#define USEGLOW + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/58.shader_test b/shaders/nexuiz/58.shader_test new file mode 100644 index 0000000..96cdc06 --- /dev/null +++ b/shaders/nexuiz/58.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTDIRECTION +#define USEDIFFUSE + + + + + + + + + + +#define USEGLOW + +#define USESPECULAR + + +#define USEOFFSETMAPPING + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTDIRECTION +#define USEDIFFUSE + + + + + + + + + + +#define USEGLOW + +#define USESPECULAR + + +#define USEOFFSETMAPPING + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/6.frag b/shaders/nexuiz/6.frag deleted file mode 100644 index b7af492..0000000 --- a/shaders/nexuiz/6.frag +++ /dev/null @@ -1,1016 +0,0 @@ -#define FRAGMENT_SHADER -#define MODE_LIGHTDIRECTIONMAP_MODELSPACE -#define USEDIFFUSE - - - - -#define USEGLOW -#define USEBLOOM - - - -#define USEOFFSETMAPPING - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/6.shader_test b/shaders/nexuiz/6.shader_test new file mode 100644 index 0000000..7497b6b --- /dev/null +++ b/shaders/nexuiz/6.shader_test @@ -0,0 +1,22 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +uniform ivec4 color; +out ivec4 out_color; + +void main() +{ + out_color = color; +} + +[vertex shader] +#version 130 +in vec4 position; +void main() +{ + gl_Position = position; +} + + diff --git a/shaders/nexuiz/6.vert b/shaders/nexuiz/6.vert deleted file mode 100644 index d17ad7e..0000000 --- a/shaders/nexuiz/6.vert +++ /dev/null @@ -1,1016 +0,0 @@ -#define VERTEX_SHADER -#define MODE_LIGHTDIRECTIONMAP_MODELSPACE -#define USEDIFFUSE - - - - -#define USEGLOW -#define USEBLOOM - - - -#define USEOFFSETMAPPING - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/61.shader_test b/shaders/nexuiz/61.shader_test new file mode 100644 index 0000000..fb18549 --- /dev/null +++ b/shaders/nexuiz/61.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTDIRECTION +#define USEDIFFUSE + + +#define USECOLORMAPPING + + + + + + + + + +#define USESPECULAR + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTDIRECTION +#define USEDIFFUSE + + +#define USECOLORMAPPING + + + + + + + + + +#define USESPECULAR + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/64.shader_test b/shaders/nexuiz/64.shader_test new file mode 100644 index 0000000..db4db4e --- /dev/null +++ b/shaders/nexuiz/64.shader_test @@ -0,0 +1,3156 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTDIRECTION +#define USEDIFFUSE + + + + + + + + + + +#define USEGLOW + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normaautoswitch turned on +lize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTDIRECTION +#define USEDIFFUSE + + + + + + + + + + +#define USEGLOW + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/67.shader_test b/shaders/nexuiz/67.shader_test new file mode 100644 index 0000000..1a32d59 --- /dev/null +++ b/shaders/nexuiz/67.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTDIRECTION +#define USEDIFFUSE + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTDIRECTION +#define USEDIFFUSE + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/7.frag b/shaders/nexuiz/7.frag deleted file mode 100644 index 31c1583..0000000 --- a/shaders/nexuiz/7.frag +++ /dev/null @@ -1,1017 +0,0 @@ -#define FRAGMENT_SHADER -#define MODE_LIGHTDIRECTION -#define USEDIFFUSE - -#define USECOLORMAPPING -#define USESATURATION - - -#define USEGLOW -#define USEBLOOM -#define USESPECULAR -#define USEPOSTPROCESSING - -#define USEOFFSETMAPPING - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/7.shader_test b/shaders/nexuiz/7.shader_test new file mode 100644 index 0000000..7efb5d9 --- /dev/null +++ b/shaders/nexuiz/7.shader_test @@ -0,0 +1,3158 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_GENERIC +#define USEDIFFUSE + +#define USEVIEWTINT +#define USECOLORMAPPING + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coCDAudio_Init: No CD in player. +Can't get initial CD volume +CD Audio Initialized +eff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_GENERIC +#define USEDIFFUSE + +#define USEVIEWTINT +#define USECOLORMAPPING + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/7.vert b/shaders/nexuiz/7.vert deleted file mode 100644 index e5bb9c3..0000000 --- a/shaders/nexuiz/7.vert +++ /dev/null @@ -1,1017 +0,0 @@ -#define VERTEX_SHADER -#define MODE_LIGHTDIRECTION -#define USEDIFFUSE - -#define USECOLORMAPPING -#define USESATURATION - - -#define USEGLOW -#define USEBLOOM -#define USESPECULAR -#define USEPOSTPROCESSING - -#define USEOFFSETMAPPING - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/70.shader_test b/shaders/nexuiz/70.shader_test new file mode 100644 index 0000000..3e4280f --- /dev/null +++ b/shaders/nexuiz/70.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTDIRECTION +#define USEDIFFUSE + + + + + + + + + + +#define USEGLOW + +#define USESPECULAR + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTDIRECTION +#define USEDIFFUSE + + + + + + + + + + +#define USEGLOW + +#define USESPECULAR + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/73.shader_test b/shaders/nexuiz/73.shader_test new file mode 100644 index 0000000..e473cb8 --- /dev/null +++ b/shaders/nexuiz/73.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTSOURCE +#define USEDIFFUSE + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTSOURCE +#define USEDIFFUSE + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/76.shader_test b/shaders/nexuiz/76.shader_test new file mode 100644 index 0000000..5ffb900 --- /dev/null +++ b/shaders/nexuiz/76.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTSOURCE +#define USEDIFFUSE + + + + + + + + + + + + +#define USESPECULAR + + +#define USEOFFSETMAPPING + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTSOURCE +#define USEDIFFUSE + + + + + + + + + + + + +#define USESPECULAR + + +#define USEOFFSETMAPPING + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/79.shader_test b/shaders/nexuiz/79.shader_test new file mode 100644 index 0000000..787efc6 --- /dev/null +++ b/shaders/nexuiz/79.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTSOURCE +#define USEDIFFUSE + + + + + + + + + + + + + + + + + + + + + +#define USEALPHAKILL + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTSOURCE +#define USEDIFFUSE + + + + + + + + + + + + + + + + + + + + + +#define USEALPHAKILL + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/8.frag b/shaders/nexuiz/8.frag deleted file mode 100644 index 26a79b3..0000000 --- a/shaders/nexuiz/8.frag +++ /dev/null @@ -1,1015 +0,0 @@ -#define FRAGMENT_SHADER -#define MODE_FLATCOLOR - - - - - - - - - -#define USEOFFSETMAPPING - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/8.vert b/shaders/nexuiz/8.vert deleted file mode 100644 index 4254bea..0000000 --- a/shaders/nexuiz/8.vert +++ /dev/null @@ -1,1015 +0,0 @@ -#define VERTEX_SHADER -#define MODE_FLATCOLOR - - - - - - - - - -#define USEOFFSETMAPPING - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/82.shader_test b/shaders/nexuiz/82.shader_test new file mode 100644 index 0000000..ef2282f --- /dev/null +++ b/shaders/nexuiz/82.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTSOURCE +#define USEDIFFUSE + + +#define USECOLORMAPPING + + + + + + + + + +#define USESPECULAR + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTSOURCE +#define USEDIFFUSE + + +#define USECOLORMAPPING + + + + + + + + + +#define USESPECULAR + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/85.shader_test b/shaders/nexuiz/85.shader_test new file mode 100644 index 0000000..03acf1a --- /dev/null +++ b/shaders/nexuiz/85.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTSOURCE +#define USEDIFFUSE + + + + + + + + + + + + +#define USESPECULAR + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTSOURCE +#define USEDIFFUSE + + + + + + + + + + + + +#define USESPECULAR + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/88.shader_test b/shaders/nexuiz/88.shader_test new file mode 100644 index 0000000..1833954 --- /dev/null +++ b/shaders/nexuiz/88.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTSOURCE +#define USEDIFFUSE + + + + + + + + + + + + + + + +#define USEOFFSETMAPPING + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTSOURCE +#define USEDIFFUSE + + + + + + + + + + + + + + + +#define USEOFFSETMAPPING + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/9.frag b/shaders/nexuiz/9.frag deleted file mode 100644 index a2c1937..0000000 --- a/shaders/nexuiz/9.frag +++ /dev/null @@ -1,1016 +0,0 @@ -#define FRAGMENT_SHADER -#define MODE_FLATCOLOR - - -#define USECOLORMAPPING -#define USESATURATION - - - - - - -#define USEOFFSETMAPPING - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/9.vert b/shaders/nexuiz/9.vert deleted file mode 100644 index e06061c..0000000 --- a/shaders/nexuiz/9.vert +++ /dev/null @@ -1,1016 +0,0 @@ -#define VERTEX_SHADER -#define MODE_FLATCOLOR - - -#define USECOLORMAPPING -#define USESATURATION - - - - - - -#define USEOFFSETMAPPING - - - - - - - - -// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader -// written by Forest 'LordHavoc' Hale - -// enable various extensions depending on permutation: - -#ifdef USESHADOWMAPRECT -# extension GL_ARB_texture_rectangle : enable -#endif - -#ifdef USESHADOWMAP2D -# ifdef GL_EXT_gpu_shader4 -# extension GL_EXT_gpu_shader4 : enable -# endif -# ifdef GL_ARB_texture_gather -# extension GL_ARB_texture_gather : enable -# else -# ifdef GL_AMD_texture_texture4 -# extension GL_AMD_texture_texture4 : enable -# endif -# endif -#endif - -#ifdef USESHADOWMAPCUBE -# extension GL_EXT_gpu_shader4 : enable -#endif - -#ifdef USESHADOWSAMPLER -# extension GL_ARB_shadow : enable -#endif - -// common definitions between vertex shader and fragment shader: - -//#ifdef __GLSL_CG_DATA_TYPES -//# define myhalf half -//# define myhalf2 half2 -//# define myhalf3half3 -//# define myhalf4 half4 -//#else -# define myhalf float -# define myhalf2 vec2 -# define myhalf3 vec3 -# define myhalf4 vec4 -//#endif - -#ifdef MODE_DEPTH_OR_SHADOW - -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); -} -# endif - -#else -#ifdef MODE_SHOWDEPTH -# ifdef VERTEX_SHADER -void main(void) -{ - gl_Position = ftransform(); - gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); -} -# endif -# ifdef FRAGMENT_SHADER -void main(void) -{ - gl_FragColor = gl_Color; -} -# endif - -#else // !MODE_SHOWDEPTH - -#ifdef MODE_POSTPROCESS -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; - gl_Position = ftransform(); - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -#ifdef USEBLOOM - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -#endif -} -# endif -# ifdef FRAGMENT_SHADER - -uniform sampler2D Texture_First; -#ifdef USEBLOOM -uniform sampler2D Texture_Second; -#endif -#ifdef USEGAMMARAMPS -uniform sampler2D Texture_GammaRamps; -#endif -#ifdef USESATURATION -uniform float Saturation; -#endif -#ifdef USEVIEWTINT -uniform vec4 TintColor; -#endif -//uncomment these if you want to use them: -uniform vec4 UserVec1; -// uniform vec4 UserVec2; -// uniform vec4 UserVec3; -// uniform vec4 UserVec4; -// uniform float ClientTime; -uniform vec2 PixelSize; -void main(void) -{ - gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy); -#ifdef USEBLOOM - gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy); -#endif -#ifdef USEVIEWTINT - gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a); -#endif - -#ifdef USEPOSTPROCESSING -// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want -// 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 - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; - gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; - gl_FragColor /= (1 + 5 * UserVec1.y); -#endif - -#ifdef USESATURATION - //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter - myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114)); - //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation; - gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation); -#endif - -#ifdef USEGAMMARAMPS - gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r; - gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g; - gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b; -#endif -} -# endif - - -#else -#ifdef MODE_GENERIC -# ifdef VERTEX_SHADER -void main(void) -{ - gl_FrontColor = gl_Color; -# ifdef USEDIFFUSE - gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -# endif -# ifdef USESPECULAR - gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; -# endif - gl_Position = ftransform(); -} -# endif -# ifdef FRAGMENT_SHADER - -# ifdef USEDIFFUSE -uniform sampler2D Texture_First; -# endif -# ifdef USESPECULAR -uniform sampler2D Texture_Second; -# endif - -void main(void) -{ - gl_FragColor = gl_Color; -# ifdef USEDIFFUSE - gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy); -# endif - -# ifdef USESPECULAR - vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy); -# endif -# ifdef USECOLORMAPPING - gl_FragColor *= tex2; -# endif -# ifdef USEGLOW - gl_FragColor += tex2; -# endif -# ifdef USEVERTEXTEXTUREBLEND - gl_FragColor = mix(gl_FragColor, tex2, tex2.a); -# endif -} -# endif - -#else // !MODE_GENERIC - -varying vec2 TexCoord; -#ifdef USEVERTEXTEXTUREBLEND -varying vec2 TexCoord2; -#endif -varying vec2 TexCoordLightmap; - -#ifdef MODE_LIGHTSOURCE -varying vec3 CubeVector; -#endif - -#ifdef MODE_LIGHTSOURCE -varying vec3 LightVector; -#endif -#ifdef MODE_LIGHTDIRECTION -varying vec3 LightVector; -#endif - -varying vec3 EyeVector; -#ifdef USEFOG -varying vec3 EyeVectorModelSpace; -#endif - -varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent) -varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal) -varying vec3 VectorR; // direction of R texcoord (surface normal) - -#ifdef MODE_WATER -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef MODE_REFRACTION -varying vec4 ModelViewProjectionPosition; -#endif -#ifdef USEREFLECTION -varying vec4 ModelViewProjectionPosition; -#endif - - - - - -// vertex shader specific: -#ifdef VERTEX_SHADER - -uniform vec3 LightPosition; -uniform vec3 EyePosition; -uniform vec3 LightDir; - -// 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 - -void main(void) -{ - gl_FrontColor = gl_Color; - // copy the surface texcoord - TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); -#ifdef USEVERTEXTEXTUREBLEND - TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0); -#endif -#ifndef MODE_LIGHTSOURCE -# ifndef MODE_LIGHTDIRECTION - TexCoordLightmap = vec2(gl_MultiTexCoord4); -# endif -#endif - -#ifdef MODE_LIGHTSOURCE - // transform vertex position into light attenuation/cubemap space - // (-1 to +1 across the light box) - CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex); - - // transform unnormalized light direction into tangent space - // (we use unnormalized to ensure that it interpolates correctly and then - // normalize it per pixel) - vec3 lightminusvertex = LightPosition - gl_Vertex.xyz; - LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz); - LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz); - LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz); -#endif - -#ifdef MODE_LIGHTDIRECTION - LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz); - LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz); - LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz); -#endif - - // transform unnormalized eye direction into tangent space -#ifndef USEFOG - vec3 EyeVectorModelSpace; -#endif - EyeVectorModelSpace = EyePosition - gl_Vertex.xyz; - EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz); - EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz); - EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz); - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - VectorS = gl_MultiTexCoord1.xyz; - VectorT = gl_MultiTexCoord2.xyz; - VectorR = gl_MultiTexCoord3.xyz; -#endif - -//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION) -// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix; -// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition; -//#endif - -// transform vertex to camera space, using ftransform to match non-VS - // rendering - gl_Position = ftransform(); - -#ifdef MODE_WATER - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef MODE_REFRACTION - ModelViewProjectionPosition = gl_Position; -#endif -#ifdef USEREFLECTION - ModelViewProjectionPosition = gl_Position; -#endif -} - -#endif // VERTEX_SHADER - - - - -// fragment shader specific: -#ifdef FRAGMENT_SHADER - -// 13 textures, we can only use up to 16 on DX9-class hardware -uniform sampler2D Texture_Normal; -uniform sampler2D Texture_Color; -uniform sampler2D Texture_Gloss; -uniform sampler2D Texture_Glow; -uniform sampler2D Texture_SecondaryNormal; -uniform sampler2D Texture_SecondaryColor; -uniform sampler2D Texture_SecondaryGloss; -uniform sampler2D Texture_SecondaryGlow; -uniform sampler2D Texture_Pants; -uniform sampler2D Texture_Shirt; -uniform sampler2D Texture_FogMask; -uniform sampler2D Texture_Lightmap; -uniform sampler2D Texture_Deluxemap; -uniform sampler2D Texture_Refraction; -uniform sampler2D Texture_Reflection; -uniform sampler2D Texture_Attenuation; -uniform samplerCube Texture_Cube; - -#define showshadowmap 0 - -#ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER -uniform sampler2DRectShadow Texture_ShadowMapRect; -# else -uniform sampler2DRect Texture_ShadowMapRect; -# endif -#endif - -#ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER -uniform sampler2DShadow Texture_ShadowMap2D; -# else -uniform sampler2D Texture_ShadowMap2D; -# endif -#endif - -#ifdef USESHADOWMAPVSDCT -uniform samplerCube Texture_CubeProjection; -#endif - -#ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER -uniform samplerCubeShadow Texture_ShadowMapCube; -# else -uniform samplerCube Texture_ShadowMapCube; -# endif -#endif - -uniform myhalf3 LightColor; -uniform myhalf3 AmbientColor; -uniform myhalf3 DiffuseColor; -uniform myhalf3 SpecularColor; -uniform myhalf3 Color_Pants; -uniform myhalf3 Color_Shirt; -uniform myhalf3 FogColor; - -uniform myhalf4 TintColor; - - -//#ifdef MODE_WATER -uniform vec4 DistortScaleRefractReflect; -uniform vec4 ScreenScaleRefractReflect; -uniform vec4 ScreenCenterRefractReflect; -uniform myhalf4 RefractColor; -uniform myhalf4 ReflectColor; -uniform myhalf ReflectFactor; -uniform myhalf ReflectOffset; -//#else -//# ifdef MODE_REFRACTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 RefractColor; -//# ifdef USEREFLECTION -//uniform myhalf4 ReflectColor; -//# endif -//# else -//# ifdef USEREFLECTION -//uniform vec4 DistortScaleRefractReflect; -//uniform vec4 ScreenScaleRefractReflect; -//uniform vec4 ScreenCenterRefractReflect; -//uniform myhalf4 ReflectColor; -//# endif -//# endif -//#endif - -uniform myhalf GlowScale; -uniform myhalf SceneBrightness; - -uniform float OffsetMapping_Scale; -uniform float OffsetMapping_Bias; -uniform float FogRangeRecip; - -uniform myhalf AmbientScale; -uniform myhalf DiffuseScale; -uniform myhalf SpecularScale; -uniform myhalf SpecularPower; - -#ifdef USEOFFSETMAPPING -vec2 OffsetMapping(vec2 TexCoord) -{ -#ifdef USEOFFSETMAPPING_RELIEFMAPPING - // 14 sample relief mapping: linear search and then binary search - // this basically steps forward a small amount repeatedly until it finds - // itself inside solid, then jitters forward and back using decreasing - // amounts to find the impact - //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1); - //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1); - vec3 RT = vec3(TexCoord, 1); - OffsetVector *= 0.1; - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625); - RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125); - return RT.xy; -#else - // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits) - // this basically moves forward the full distance, and then backs up based - // on height of samples - //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1)); - //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1)); - vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1)); - TexCoord += OffsetVector; - OffsetVector *= 0.333; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a; - return TexCoord; -#endif -} -#endif // USEOFFSETMAPPING - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE) -uniform vec2 ShadowMap_TextureScale; -uniform vec4 ShadowMap_Parameters; -#endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) -vec3 GetShadowMapTC2D(vec3 dir) -{ - vec3 adir = abs(dir); -# ifndef USESHADOWMAPVSDCT - vec2 tc; - vec2 offset; - float ma; - if (adir.x > adir.y) - { - if (adir.x > adir.z) // X - { - ma = adir.x; - tc = dir.zy; - offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - else - { - if (adir.y > adir.z) // Y - { - ma = adir.y; - tc = dir.xz; - offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5); - } - else // Z - { - ma = adir.z; - tc = dir.xy; - offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5); - } - } - - vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += offset * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# else - vec4 proj = textureCube(Texture_CubeProjection, dir); - float ma = max(max(adir.x, adir.y), adir.z); - vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma; - stc.xy += proj.zw * ShadowMap_Parameters.y; - stc.z += ShadowMap_Parameters.z; -# if showshadowmap - stc.xy *= ShadowMap_TextureScale; -# endif - return stc; -# endif -} -#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) - -#ifdef USESHADOWMAPCUBE -vec4 GetShadowMapTCCube(vec3 dir) -{ - vec3 adir = abs(dir); - return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z)); -} -#endif - -#if !showshadowmap -# ifdef USESHADOWMAPRECT -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; -# ifdef USESHADOWSAMPLER - -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r - 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))); -# else - f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r; -# endif - -# else - -# ifdef USESHADOWMAPPCF -# if USESHADOWMAPPCF > 1 -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r - vec2 offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# else - f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r); -# endif - -# endif - return f; -} -# endif - -# ifdef USESHADOWMAP2D -float ShadowMapCompare(vec3 dir) -{ - vec3 shadowmaptc = GetShadowMapTC2D(dir); - float f; - -# ifdef USESHADOWSAMPLER -# ifdef USESHADOWMAPPCF -# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; - 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))); -# else - f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r; -# endif -# else -# ifdef USESHADOWMAPPCF -# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) -# ifdef GL_ARB_texture_gather -# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y)) -# else -# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale) -# endif - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); - vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); - vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); - vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); - vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + - mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else -# ifdef GL_EXT_gpu_shader4 -# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r -# else -# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r -# endif -# if USESHADOWMAPPCF > 1 - vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); - center *= ShadowMap_TextureScale; - 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))); - 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))); - 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))); - 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))); - vec4 cols = row2 + row3 + mix(row1, row4, offset.y); - f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); -# else - vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); - vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); - vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); - vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); - vec3 cols = row2 + mix(row1, row3, offset.y); - f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); -# endif -# endif -# else - f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); -# endif -# endif - return f; -} -# endif - -# ifdef USESHADOWMAPCUBE -float ShadowMapCompare(vec3 dir) -{ - // apply depth texture cubemap as light filter - vec4 shadowmaptc = GetShadowMapTCCube(dir); - float f; -# ifdef USESHADOWSAMPLER - f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r; -# else - f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r); -# endif - return f; -} -# endif -#endif - -#ifdef MODE_WATER - -// water pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); - f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); - float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset; - gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel); -} - -#else // !MODE_WATER -#ifdef MODE_REFRACTION - -// refraction pass -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); - //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor; -} - -#else // !MODE_REFRACTION -void main(void) -{ -#ifdef USEOFFSETMAPPING - // apply offsetmapping - vec2 TexCoordOffset = OffsetMapping(TexCoord); -#define TexCoord TexCoordOffset -#endif - - // combine the diffuse textures (base, pants, shirt) - myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord)); -#ifdef USECOLORMAPPING - color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt; -#endif -#ifdef USEVERTEXTEXTUREBLEND - myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0)); - //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0)); - //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5; - color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); - color.a = 1.0; - //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend); -#endif - -#ifdef USEDIFFUSE - // get the surface normal and the gloss color -# ifdef USEVERTEXTEXTUREBLEND - myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend); -# endif -# else - myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5)); -# ifdef USESPECULAR - myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord)); -# endif -# endif -#endif - - - -#ifdef MODE_LIGHTSOURCE - // light source - - // calculate surface normal, light normal, and specular normal - // compute color intensity for the two textures (colormap and glossmap) - // scale by light color and attenuation as efficiently as possible - // (do as much scalar math as possible rather than vector math) -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR -# ifndef USEEXACTSPECULARMATH - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - -# endif - // calculate directional shading -# ifdef USEEXACTSPECULARMATH - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor); -# else - color.rgb = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * glosscolor); -# endif -# else -# ifdef USEDIFFUSE - // calculate directional shading - color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)))); -# else - // calculate directionless shading - color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); -# endif -# endif - -#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D) -#if !showshadowmap - color.rgb *= ShadowMapCompare(CubeVector); -#endif -#endif - -# ifdef USECUBEFILTER - // apply light cubemap filter - //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector)); - color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector)); -# endif -#endif // MODE_LIGHTSOURCE - - - - -#ifdef MODE_LIGHTDIRECTION - // directional model lighting -# ifdef USEDIFFUSE - // get the light normal - myhalf3 diffusenormal = myhalf3(normalize(LightVector)); -# endif -# ifdef USESPECULAR - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# ifdef USEEXACTSPECULARMATH - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector))); - color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# else -# ifdef USEDIFFUSE - - // calculate directional shading - color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)); -# else - color.rgb *= AmbientColor; -# endif -# endif -#endif // MODE_LIGHTDIRECTION - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE - // deluxemap lightmapping using light vectors in modelspace (evil q3map2) - - // get the light normal - myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - myhalf3 diffusenormal; - diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS)); - diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT)); - diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR)); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar - // is used (the lightmap and deluxemap coords correspond to virtually random coordinates - // on that luxel, and NOT to its center, because recursive triangle subdivision is used - // to map the luxels to coordinates on the draw surfaces), which also causes - // deluxemaps to be wrong because light contributions from the wrong side of the surface - // are added up. To prevent divisions by zero or strong exaggerations, a max() - // nudge is done here at expense of some additional fps. This is ONLY needed for - // deluxemaps, tangentspace deluxemap avoid this problem by design. - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0))); - // 0.25 supports up to 75.5 degrees normal/deluxe angle -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE - - - - -#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) - - // get the light normal - myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0); - // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) - myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0))); -# ifdef USESPECULAR -# ifdef USEEXACTSPECULARMATH - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower); -# else - myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector)))); - tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower); -# endif -# endif - - // apply lightmap color - color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)); -#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE - - - - -#ifdef MODE_LIGHTMAP - // apply lightmap color - color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_LIGHTMAP - - - - -#ifdef MODE_VERTEXCOLOR - // apply lightmap color - color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale; -#endif // MODE_VERTEXCOLOR - - - - -#ifdef MODE_FLATCOLOR -#endif // MODE_FLATCOLOR - - - - - - - - color *= TintColor; - -#ifdef USEGLOW -#ifdef USEVERTEXTEXTUREBLEND - color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend); -#else - color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowScale; -#endif -#endif - - color.rgb *= SceneBrightness; - - // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately -#ifdef USEFOG - color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)))); -#endif - - // 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 -#ifdef USEREFLECTION - vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); - //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; - vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; - vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; - // FIXME temporary hack to detect the case that the reflection - // gets blackened at edges due to leaving the area that contains actual - // content. - // Remove this 'ack once we have a better way to stop this thing from - // 'appening. - float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); - f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); - ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); - color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); -#endif - - gl_FragColor = vec4(color); - -#if showshadowmap -# ifdef USESHADOWMAPRECT -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif -# ifdef USESHADOWMAP2D -# ifdef USESHADOWSAMPLER - gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz); -# else - gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy); -# endif -# endif - -# ifdef USESHADOWMAPCUBE -# ifdef USESHADOWSAMPLER - gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector)); -# else - gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz); -# endif -# endif -#endif -} -#endif // !MODE_REFRACTION -#endif // !MODE_WATER - -#endif // FRAGMENT_SHADER - -#endif // !MODE_GENERIC -#endif // !MODE_POSTPROCESS -#endif // !MODE_SHOWDEPTH -#endif // !MODE_DEPTH_OR_SHADOW - diff --git a/shaders/nexuiz/91.shader_test b/shaders/nexuiz/91.shader_test new file mode 100644 index 0000000..5300b7e --- /dev/null +++ b/shaders/nexuiz/91.shader_test @@ -0,0 +1,3157 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTSOURCE +#define USEDIFFUSE + + +#define USECOLORMAPPING + + + + + + + + + +#define USESPECULAR + + +#define USEOFFSETMAPPING + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5[BOT]Roadkill drew first blood +[BOT]Darkness was gunned by [BOT]Roadkill +)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTSOURCE +#define USEDIFFUSE + + +#define USECOLORMAPPING + + + + + + + + + +#define USESPECULAR + + +#define USEOFFSETMAPPING + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/94.shader_test b/shaders/nexuiz/94.shader_test new file mode 100644 index 0000000..b80972e --- /dev/null +++ b/shaders/nexuiz/94.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_LIGHTDIRECTION +#define USEDIFFUSE + + +#define USECOLORMAPPING + + + + + + + + + + + + +#define USEOFFSETMAPPING + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_LIGHTDIRECTION +#define USEDIFFUSE + + +#define USECOLORMAPPING + + + + + + + + + + + + +#define USEOFFSETMAPPING + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + diff --git a/shaders/nexuiz/97.shader_test b/shaders/nexuiz/97.shader_test new file mode 100644 index 0000000..ee19dd4 --- /dev/null +++ b/shaders/nexuiz/97.shader_test @@ -0,0 +1,3155 @@ +[require] +GLSL >= 1.10 + +[fragment shader] +#version 130 +#define GLSL130 +#define FRAGMENT_SHADER +#define MODE_VERTEXCOLOR + + + + + + + + + + + +#define USEGLOW + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + +[vertex shader] +#version 130 +#define GLSL130 +#define VERTEX_SHADER +#define MODE_VERTEXCOLOR + + + + + + + + + + + +#define USEGLOW + + + + + + + + + + + + + + + + + + + + + + + + + + +#define USESHADOWMAPPCF 1 + + +// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader +// written by Forest 'LordHavoc' Hale +// shadowmapping enhancements by Lee 'eihrul' Salzman + +#ifdef GLSL130 +precision highp float; +# ifdef VERTEX_SHADER +# define dp_varying out +# define dp_attribute in +# endif +# ifdef FRAGMENT_SHADER +out vec4 dp_FragColor; +# define dp_varying in +# define dp_attribute in +# endif +# define dp_offsetmapping_dFdx dFdx +# define dp_offsetmapping_dFdy dFdy +# define dp_textureGrad textureGrad +# define dp_textureOffset(a,b,c,d) textureOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture +# define dp_texture3D texture +# define dp_textureCube texture +# define dp_shadow2D(a,b) float(texture(a,b)) +#else +# ifdef FRAGMENT_SHADER +# define dp_FragColor gl_FragColor +# endif +# define dp_varying varying +# define dp_attribute attribute +# define dp_offsetmapping_dFdx(a) vec2(0.0, 0.0) +# define dp_offsetmapping_dFdy(a) vec2(0.0, 0.0) +# define dp_textureGrad(a,b,c,d) texture2D(a,b) +# define dp_textureOffset(a,b,c,d) texture2DOffset(a,b,ivec2(c,d)) +# define dp_texture2D texture2D +# define dp_texture3D texture3D +# define dp_textureCube textureCube +# define dp_shadow2D(a,b) float(shadow2D(a,b)) +#endif + +// GL ES and GLSL130 shaders use precision modifiers, standard GL does not +// in GLSL130 we don't use them though because of syntax differences (can't use precision with inout) +#ifndef GL_ES +#define lowp +#define mediump +#define highp +#endif + +#ifdef USEDEPTHRGB + // for 565 RGB we'd need to use different multipliers +#define decodedepthmacro(d) dot((d).rgb, vec3(1.0, 255.0 / 65536.0, 255.0 / 16777215.0)) +#define encodedepthmacro(d) (vec4(d, d*256.0, d*65536.0, 0.0) - floor(vec4(d, d*256.0, d*65536.0, 0.0))) +#endif + +#ifdef VERTEX_SHADER +dp_attribute vec4 Attrib_Position; // vertex +dp_attribute vec4 Attrib_Color; // color +dp_attribute vec4 Attrib_TexCoord0; // material texcoords +dp_attribute vec3 Attrib_TexCoord1; // svector +dp_attribute vec3 Attrib_TexCoord2; // tvector +dp_attribute vec3 Attrib_TexCoord3; // normal +dp_attribute vec4 Attrib_TexCoord4; // lightmap texcoords +#endif +dp_varying mediump vec4 VertexColor; + +#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE) +# define USEFOG +#endif +#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) +# define USELIGHTMAP +#endif +#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT) || defined(USEFOG) +# define USEEYEVECTOR +#endif + +#ifdef USESHADOWMAP2D +# ifdef GL_EXT_gpu_shader4 +# extension GL_EXT_gpu_shader4 : enable +# endif +# ifdef GL_ARB_texture_gather +# extension GL_ARB_texture_gather : enable +# else +# ifdef GL_AMD_texture_texture4 +# extension GL_AMD_texture_texture4 : enable +# endif +# endif +#endif + +//#ifdef USESHADOWSAMPLER +//# extension GL_ARB_shadow : enable +//#endif + +//#ifdef __GLSL_CG_DATA_TYPES +//# define myhalf half +//# define myhalf2 half2 +//# define myhalf3 half3 +//# define myhalf4 half4 +//# define cast_myhalf half +//# define cast_myhalf2 half2 +//# define cast_myhalf3 half3 +//# define cast_myhalf4 half4 +//#else +# define myhalf mediump float +# define myhalf2 mediump vec2 +# define myhalf3 mediump vec3 +# define myhalf4 mediump vec4 +# define cast_myhalf float +# define cast_myhalf2 vec2 +# define cast_myhalf3 vec3 +# define cast_myhalf4 vec4 +//#endif + +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewProjectionMatrix; +#endif + +#ifdef VERTEX_SHADER +#ifdef USETRIPPY +// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE +// tweaked scale +uniform highp float ClientTime; +vec4 TrippyVertex(vec4 position) +{ + float worldTime = ClientTime; + // tweaked for Quake + worldTime *= 10.0; + position *= 0.125; + //~tweaked for Quake + float distanceSquared = (position.x * position.x + position.z * position.z); + position.y += 5.0*sin(distanceSquared*sin(worldTime/143.0)/1000.0); + float y = position.y; + float x = position.x; + float om = sin(distanceSquared*sin(worldTime/256.0)/5000.0) * sin(worldTime/200.0); + position.y = x*sin(om)+y*cos(om); + position.x = x*cos(om)-y*sin(om); + return position; +} +#endif +#endif + +#ifdef MODE_DEPTH_OR_SHADOW +dp_varying highp float Depth; +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = gl_Position.z; +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEDEPTHRGB + dp_FragColor = encodedepthmacro(Depth); +#else + dp_FragColor = vec4(1.0,1.0,1.0,1.0); +#endif +} +#endif +#else // !MODE_DEPTH_ORSHADOW + + + + +#ifdef MODE_SHOWDEPTH +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + VertexColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0); +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +void main(void) +{ + dp_FragColor = VertexColor; +} +#endif +#else // !MODE_SHOWDEPTH + + + + +#ifdef MODE_POSTPROCESS +dp_varying mediump vec2 TexCoord1; +dp_varying mediump vec2 TexCoord2; + +#ifdef VERTEX_SHADER +void main(void) +{ + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + TexCoord1 = Attrib_TexCoord0.xy; +#ifdef USEBLOOM + TexCoord2 = Attrib_TexCoord4.xy; +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +#ifdef USEBLOOM +uniform sampler2D Texture_Second; +uniform mediump vec4 BloomColorSubtract; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif +#ifdef USESATURATION +uniform mediump float Saturation; +#endif +#ifdef USEVIEWTINT +uniform mediump vec4 ViewTintColor; +#endif +//uncomment these if you want to use them: +uniform mediump vec4 UserVec1; +uniform mediump vec4 UserVec2; +// uniform mediump vec4 UserVec3; +// uniform mediump vec4 UserVec4; +// uniform highp float ClientTime; +uniform mediump vec2 PixelSize; +void main(void) +{ + dp_FragColor = dp_texture2D(Texture_First, TexCoord1); +#ifdef USEBLOOM + dp_FragColor += max(vec4(0,0,0,0), dp_texture2D(Texture_Second, TexCoord2) - BloomColorSubtract); +#endif +#ifdef USEVIEWTINT + dp_FragColor = mix(dp_FragColor, ViewTintColor, ViewTintColor.a); +#endif + +#ifdef USEPOSTPROCESSING +// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want +// 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 + float sobel = 1.0; + // vec2 ts = textureSize(Texture_First, 0); + // vec2 px = vec2(1/ts.x, 1/ts.y); + vec2 px = PixelSize; + vec3 x1 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + vec3 x2 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, 0.0)).rgb; + vec3 x3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 x4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 x5 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, 0.0)).rgb; + vec3 x6 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y1 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb; + vec3 y2 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0,-px.y)).rgb; + vec3 y3 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb; + vec3 y4 = dp_texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb; + vec3 y5 = dp_texture2D(Texture_First, TexCoord1 + vec2( 0.0, px.y)).rgb; + vec3 y6 = dp_texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb; + float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1); + float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2); + float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3); + float px4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x4); + float px5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), x5); + float px6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), x6); + float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1); + float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2); + float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3); + float py4 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y4); + float py5 = 2.0 * dot(vec3(0.3, 0.59, 0.11), y5); + float py6 = 1.0 * dot(vec3(0.3, 0.59, 0.11), y6); + sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6); + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y; + dp_FragColor += dp_texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y; + dp_FragColor /= (1.0 + 5.0 * UserVec1.y); + dp_FragColor.rgb = dp_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y; +#endif + +#ifdef USESATURATION + //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter + float y = dot(dp_FragColor.rgb, vec3(0.299, 0.587, 0.114)); + // 'vampire sight' effect, wheres red is compensated + #ifdef SATURATION_REDCOMPENSATE + float rboost = max(0.0, (dp_FragColor.r - max(dp_FragColor.g, dp_FragColor.b))*(1.0 - Saturation)); + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + dp_FragColor.r += rboost; + #else + // normal desaturation + //dp_FragColor = vec3(y) + (dp_FragColor.rgb - vec3(y)) * Saturation; + dp_FragColor.rgb = mix(vec3(y), dp_FragColor.rgb, Saturation); + #endif +#endif + +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_POSTPROCESS + + + + +#ifdef MODE_GENERIC +#ifdef USEDIFFUSE +dp_varying mediump vec2 TexCoord1; +#endif +#ifdef USESPECULAR +dp_varying mediump vec2 TexCoord2; +#endif +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; +#ifdef USEDIFFUSE + TexCoord1 = Attrib_TexCoord0.xy; +#endif +#ifdef USESPECULAR + TexCoord2 = Attrib_TexCoord1.xy; +#endif + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +#ifdef USEDIFFUSE +uniform sampler2D Texture_First; +#endif +#ifdef USESPECULAR +uniform sampler2D Texture_Second; +#endif +#ifdef USEGAMMARAMPS +uniform sampler2D Texture_GammaRamps; +#endif + +void main(void) +{ +#ifdef USEVIEWTINT + dp_FragColor = VertexColor; +#else + dp_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +#endif +#ifdef USEDIFFUSE +# ifdef USEREFLECTCUBE + // suppress texture alpha + dp_FragColor.rgb *= dp_texture2D(Texture_First, TexCoord1).rgb; +# else + dp_FragColor *= dp_texture2D(Texture_First, TexCoord1); +# endif +#endif + +#ifdef USESPECULAR + vec4 tex2 = dp_texture2D(Texture_Second, TexCoord2); +# ifdef USECOLORMAPPING + dp_FragColor *= tex2; +# endif +# ifdef USEGLOW + dp_FragColor += tex2; +# endif +# ifdef USEVERTEXTEXTUREBLEND + dp_FragColor = mix(dp_FragColor, tex2, tex2.a); +# endif +#endif +#ifdef USEGAMMARAMPS + dp_FragColor.r = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.r, 0)).r; + dp_FragColor.g = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.g, 0)).g; + dp_FragColor.b = dp_texture2D(Texture_GammaRamps, vec2(dp_FragColor.b, 0)).b; +#endif +} +#endif +#else // !MODE_GENERIC + + + + +#ifdef MODE_BLOOMBLUR +dp_varying mediump vec2 TexCoord; +#ifdef VERTEX_SHADER +void main(void) +{ + VertexColor = Attrib_Color; + TexCoord = Attrib_TexCoord0.xy; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_First; +uniform mediump vec4 BloomBlur_Parameters; + +void main(void) +{ + int i; + vec2 tc = TexCoord; + vec3 color = dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + for (i = 1;i < SAMPLES;i++) + { + color += dp_texture2D(Texture_First, tc).rgb; + tc += BloomBlur_Parameters.xy; + } + dp_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1); +} +#endif +#else // !MODE_BLOOMBLUR +#ifdef MODE_REFRACTION +dp_varying mediump vec2 TexCoord; +dp_varying highp vec4 ModelViewProjectionPosition; +uniform highp mat4 TexMatrix; +#ifdef VERTEX_SHADER + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w); + //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy; +#ifdef USEALPHAGENVERTEX + vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec2 distort = DistortScaleRefractReflect.xy; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor; +} +#endif +#else // !MODE_REFRACTION + + + + +#ifdef MODE_WATER +dp_varying mediump vec2 TexCoord; +dp_varying highp vec3 EyeVector; +dp_varying highp vec4 ModelViewProjectionPosition; +#ifdef VERTEX_SHADER +uniform highp vec3 EyePosition; +uniform highp mat4 TexMatrix; + +void main(void) +{ +#ifdef USEALPHAGENVERTEX + VertexColor = Attrib_Color; +#endif + TexCoord = vec2(TexMatrix * Attrib_TexCoord0); + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVector.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVector.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + ModelViewProjectionPosition = gl_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif + +#ifdef FRAGMENT_SHADER +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Refraction; +uniform sampler2D Texture_Reflection; + +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 RefractColor; +uniform mediump vec4 ReflectColor; +uniform mediump float ReflectFactor; +uniform mediump float ReflectOffset; +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif + +void main(void) +{ + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0); + // slight water animation via 2 layer scrolling (todo: tweak) +#ifdef USEALPHAGENVERTEX + vec4 distort = DistortScaleRefractReflect * VertexColor.a; + float reflectoffset = ReflectOffset * VertexColor.a; + float reflectfactor = ReflectFactor * VertexColor.a; + vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a); +#else + vec4 distort = DistortScaleRefractReflect; + float reflectoffset = ReflectOffset; + float reflectfactor = ReflectFactor; + vec4 refractcolor = RefractColor; +#endif + #ifdef USENORMALMAPSCROLLBLEND + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort; + #else + vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002); + ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f); + f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002); + ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f); + float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset; + dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel); +} +#endif +#else // !MODE_WATER + + + + +// common definitions between vertex shader and fragment shader: + +dp_varying mediump vec4 TexCoordSurfaceLightmap; +#ifdef USEVERTEXTEXTUREBLEND +dp_varying mediump vec2 TexCoord2; +#endif + +#ifdef MODE_LIGHTSOURCE +dp_varying mediump vec3 CubeVector; +#endif + +#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE) +dp_varying mediump vec3 LightVector; +#endif + +#ifdef USEEYEVECTOR +dp_varying highp vec4 EyeVectorFogDepth; +#endif + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +dp_varying highp vec4 VectorS; // direction of S texcoord (sometimes crudely called tangent) +dp_varying highp vec4 VectorT; // direction of T texcoord (sometimes crudely called binormal) +dp_varying highp vec4 VectorR; // direction of R texcoord (surface normal) +#else +# ifdef USEFOG +dp_varying highp vec3 EyeVectorModelSpace; +# endif +#endif + +#ifdef USEREFLECTION +dp_varying highp vec4 ModelViewProjectionPosition; +#endif +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform highp vec3 LightPosition; +dp_varying highp vec4 ModelViewPosition; +#endif + +#ifdef MODE_LIGHTSOURCE +uniform highp vec3 LightPosition; +#endif +uniform highp vec3 EyePosition; +#ifdef MODE_LIGHTDIRECTION +uniform highp vec3 LightDir; +#endif +uniform highp vec4 FogPlane; + +#ifdef USESHADOWMAPORTHO +dp_varying highp vec3 ShadowMapTC; +#endif + +#ifdef USEBOUNCEGRID +dp_varying highp vec3 BounceGridTexCoord; +#endif + +#ifdef MODE_DEFERREDGEOMETRY +dp_varying highp float Depth; +#endif + + + + + + +// 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 + +// fragment shader specific: +#ifdef FRAGMENT_SHADER + +uniform sampler2D Texture_Normal; +uniform sampler2D Texture_Color; +uniform sampler2D Texture_Gloss; +#ifdef USEGLOW +uniform sampler2D Texture_Glow; +#endif +#ifdef USEVERTEXTEXTUREBLEND +uniform sampler2D Texture_SecondaryNormal; +uniform sampler2D Texture_SecondaryColor; +uniform sampler2D Texture_SecondaryGloss; +#ifdef USEGLOW +uniform sampler2D Texture_SecondaryGlow; +#endif +#endif +#ifdef USECOLORMAPPING +uniform sampler2D Texture_Pants; +uniform sampler2D Texture_Shirt; +#endif +#ifdef USEFOG +#ifdef USEFOGHEIGHTTEXTURE +uniform sampler2D Texture_FogHeightTexture; +#endif +uniform sampler2D Texture_FogMask; +#endif +#ifdef USELIGHTMAP +uniform sampler2D Texture_Lightmap; +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE) +uniform sampler2D Texture_Deluxemap; +#endif +#ifdef USEREFLECTION +uniform sampler2D Texture_Reflection; +#endif + +#ifdef MODE_DEFERREDLIGHTSOURCE +uniform sampler2D Texture_ScreenNormalMap; +#endif +#ifdef USEDEFERREDLIGHTMAP +uniform sampler2D Texture_ScreenDiffuse; +uniform sampler2D Texture_ScreenSpecular; +#endif + +uniform mediump vec3 Color_Pants; +uniform mediump vec3 Color_Shirt; +uniform mediump vec3 FogColor; + +#ifdef USEFOG +uniform highp float FogRangeRecip; +uniform highp float FogPlaneViewDist; +uniform highp float FogHeightFade; +vec3 FogVertex(vec4 surfacecolor) +{ +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) + vec3 EyeVectorModelSpace = vec3(VectorS.w, VectorT.w, VectorR.w); +#endif + float FogPlaneVertexDist = EyeVectorFogDepth.w; + float fogfrac; + vec3 fc = FogColor; +#ifdef USEFOGALPHAHACK + fc *= surfacecolor.a; +#endif +#ifdef USEFOGHEIGHTTEXTURE + vec4 fogheightpixel = dp_texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade)); + fogfrac = fogheightpixel.a; + return mix(fogheightpixel.rgb * fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#else +# ifdef USEFOGOUTSIDE + fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade); +# else + fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade); +# endif + return mix(fc, surfacecolor.rgb, dp_texture2D(Texture_FogMask, cast_myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r); +#endif +} +#endif + +#ifdef USEOFFSETMAPPING +uniform mediump vec4 OffsetMapping_ScaleSteps; +uniform mediump float OffsetMapping_Bias; +#ifdef USEOFFSETMAPPING_LOD +uniform mediump float OffsetMapping_LodDistance; +#endif +vec2 OffsetMapping(vec2 TexCoord, vec2 dPdx, vec2 dPdy) +{ + float i; + // distance-based LOD +#ifdef USEOFFSETMAPPING_LOD + mediump float LODFactor = min(1.0, OffsetMapping_LodDistance / EyeVectorFogDepth.z); + mediump vec4 ScaleSteps = vec4(OffsetMapping_ScaleSteps.x, OffsetMapping_ScaleSteps.y * LODFactor, OffsetMapping_ScaleSteps.z / LODFactor, OffsetMapping_ScaleSteps.w * LODFactor); +#else + #define ScaleSteps OffsetMapping_ScaleSteps +#endif +#ifdef USEOFFSETMAPPING_RELIEFMAPPING + float f; + // 14 sample relief mapping: linear search and then binary search + // this basically steps forward a small amount repeatedly until it finds + // itself inside solid, then jitters forward and back using decreasing + // amounts to find the impact + //vec3 OffsetVector = vec3(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1), -1); + //vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1), -1); + vec3 OffsetVector = vec3(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1), -1); + vec3 RT = vec3(vec2(TexCoord.xy - OffsetVector.xy*OffsetMapping_Bias), 1); + OffsetVector *= ScaleSteps.z; + for(i = 1.0; i < ScaleSteps.y; ++i) + RT += OffsetVector * step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z); + for(i = 0.0, f = 1.0; i < ScaleSteps.w; ++i, f *= 0.5) + RT += OffsetVector * (step(dp_textureGrad(Texture_Normal, RT.xy, dPdx, dPdy).a, RT.z) * f - 0.5 * f); + return RT.xy; +#else + // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits) + //vec2 OffsetVector = vec2(EyeVectorFogDepth.xy * ((1.0 / EyeVectorFogDepth.z) * ScaleSteps.x) * vec2(-1, 1)); + //vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xy) * ScaleSteps.x * vec2(-1, 1)); + vec2 OffsetVector = vec2(normalize(EyeVectorFogDepth.xyz).xy * ScaleSteps.x * vec2(-1, 1)); + OffsetVector *= ScaleSteps.z; + for(i = 0.0; i < ScaleSteps.y; ++i) + TexCoord += OffsetVector * ((1.0 - OffsetMapping_Bias) - dp_textureGrad(Texture_Normal, TexCoord, dPdx, dPdy).a); + return TexCoord; +#endif +} +#endif // USEOFFSETMAPPING + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) +uniform sampler2D Texture_Attenuation; +uniform samplerCube Texture_Cube; +#endif + +#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO) + +#ifdef USESHADOWMAP2D +# ifdef USESHADOWSAMPLER +uniform sampler2DShadow Texture_ShadowMap2D; +# else +uniform sampler2D Texture_ShadowMap2D; +# endif +#endif + +#ifdef USESHADOWMAPVSDCT +uniform samplerCube Texture_CubeProjection; +#endif + +#if defined(USESHADOWMAP2D) +uniform mediump vec2 ShadowMap_TextureScale; +uniform mediump vec4 ShadowMap_Parameters; +#endif + +#if defined(USESHADOWMAP2D) +# ifdef USESHADOWMAPORTHO +# define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz)) +# else +# ifdef USESHADOWMAPVSDCT +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m = max(max(adir.x, adir.y), adir.z); + vec4 proj = dp_textureCube(Texture_CubeProjection, dir); +#ifdef USEDEPTHRGB + return vec3(mix(dir.xy, dir.zz, proj.xy) * (ShadowMap_Parameters.x / m) + proj.zw * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(mix(dir.xy, dir.zz, proj.xy) * mparams.x + proj.zw * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# else +vec3 GetShadowMapTC2D(vec3 dir) +{ + vec3 adir = abs(dir); + float m; vec4 proj; + if (adir.x > adir.y) { m = adir.x; proj = vec4(dir.zyx, 0.5); } else { m = adir.y; proj = vec4(dir.xzy, 1.5); } + if (adir.z > m) { m = adir.z; proj = vec4(dir, 2.5); } +#ifdef USEDEPTHRGB + return vec3(proj.xy * (ShadowMap_Parameters.x / m) + vec2(0.5,0.5) + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, m + 64.0 * ShadowMap_Parameters.w); +#else + vec2 mparams = ShadowMap_Parameters.xy / m; + return vec3(proj.xy * mparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, mparams.y + ShadowMap_Parameters.w); +#endif +} +# endif +# endif +#endif // defined(USESHADOWMAP2D) + +# ifdef USESHADOWMAP2D +float ShadowMapCompare(vec3 dir) +{ + vec3 shadowmaptc = GetShadowMapTC2D(dir); + float f; + +# ifdef USEDEPTHRGB +# ifdef USESHADOWMAPPCF +# define texval(x, y) decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)) +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# else + f = step(shadowmaptc.z, decodedepthmacro(dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale))); +# endif +# else +# ifdef USESHADOWSAMPLER +# ifdef USESHADOWMAPPCF +# define texval(x, y) dp_shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)) + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale; + 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))); +# else + f = dp_shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)); +# endif +# else +# ifdef USESHADOWMAPPCF +# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4) +# ifdef GL_ARB_texture_gather +# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y)) +# else +# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale) +# endif + vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale; +# if USESHADOWMAPPCF > 1 + vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0)); + vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0)); + vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0)); + vec4 group4 = step(shadowmaptc.z, texval(-2.0, 0.0)); + vec4 group5 = step(shadowmaptc.z, texval( 0.0, 0.0)); + vec4 group6 = step(shadowmaptc.z, texval( 2.0, 0.0)); + vec4 group7 = step(shadowmaptc.z, texval(-2.0, 2.0)); + vec4 group8 = step(shadowmaptc.z, texval( 0.0, 2.0)); + vec4 group9 = step(shadowmaptc.z, texval( 2.0, 2.0)); + vec4 locols = vec4(group1.ab, group3.ab); + vec4 hicols = vec4(group7.rg, group9.rg); + locols.yz += group2.ab; + hicols.yz += group8.rg; + vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) + + vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) + + mix(locols, hicols, offset.y); + vec4 cols = group5 + vec4(group2.rg, group8.ab); + cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x); + f = dot(cols, vec4(1.0/25.0)); +# else + vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0)); + vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0)); + vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0)); + vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0)); + vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) + + mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# endif +# else +# ifdef GL_EXT_gpu_shader4 +# define texval(x, y) dp_textureOffset(Texture_ShadowMap2D, center, x, y).r +# else +# define texval(x, y) dp_texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r +# endif +# if USESHADOWMAPPCF > 1 + vec2 center = shadowmaptc.xy - 0.5, offset = fract(center); + center *= ShadowMap_TextureScale; + 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))); + 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))); + 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))); + 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))); + vec4 cols = row2 + row3 + mix(row1, row4, offset.y); + f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0)); +# else + vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy); + vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0))); + vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0))); + vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0))); + vec3 cols = row2 + mix(row1, row3, offset.y); + f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25)); +# endif +# endif +# else + f = step(shadowmaptc.z, dp_texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r); +# endif +# endif +# endif +# ifdef USESHADOWMAPORTHO + return mix(ShadowMap_Parameters.w, 1.0, f); +# else + return f; +# endif +} +# endif +#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO) +#endif // FRAGMENT_SHADER + + + + +#ifdef MODE_DEFERREDGEOMETRY +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#ifdef USEVERTEXTEXTUREBLEND + VertexColor = Attrib_Color; + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEOFFSETMAPPING + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); + EyeVectorFogDepth.w = 0.0; +#endif + + VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)); + VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)); + VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)); + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif + Depth = (ModelViewMatrix * Attrib_Position).z; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +#endif + +#ifdef USEALPHAKILL + if (offsetMappedTexture2D(Texture_Color).a < 0.5) + discard; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + float alpha = offsetMappedTexture2D(Texture_Color).a; + float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0)); + //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0)); + //float terrainblend = float(VertexColor.a) * alpha > 0.5; +#endif + +#ifdef USEVERTEXTEXTUREBLEND + vec3 surfacenormal = mix(vec3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - vec3(0.5, 0.5, 0.5); + float a = mix(dp_texture2D(Texture_SecondaryGloss, TexCoord2).a, offsetMappedTexture2D(Texture_Gloss).a, terrainblend); +#else + vec3 surfacenormal = vec3(offsetMappedTexture2D(Texture_Normal)) - vec3(0.5, 0.5, 0.5); + float a = offsetMappedTexture2D(Texture_Gloss).a; +#endif + + vec3 pixelnormal = normalize(surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz); + dp_FragColor = vec4(pixelnormal.x, pixelnormal.y, Depth, a); +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDGEOMETRY + + + + +#ifdef MODE_DEFERREDLIGHTSOURCE +#ifdef VERTEX_SHADER +uniform highp mat4 ModelViewMatrix; +void main(void) +{ + ModelViewPosition = ModelViewMatrix * Attrib_Position; + gl_Position = ModelViewProjectionMatrix * Attrib_Position; +} +#endif // VERTEX_SHADER + +#ifdef FRAGMENT_SHADER +uniform highp mat4 ViewToLight; +// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far)); +uniform highp vec2 ScreenToDepth; +uniform myhalf3 DeferredColor_Ambient; +uniform myhalf3 DeferredColor_Diffuse; +#ifdef USESPECULAR +uniform myhalf3 DeferredColor_Specular; +uniform myhalf SpecularPower; +#endif +uniform myhalf2 PixelToScreenTexCoord; +void main(void) +{ + // calculate viewspace pixel position + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + vec3 position; + // get the geometry information (depth, normal, specular exponent) + myhalf4 normalmap = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord); + // decode viewspace pixel normal +// myhalf3 surfacenormal = normalize(normalmap.rgb - cast_myhalf3(0.5,0.5,0.5)); + myhalf3 surfacenormal = myhalf3(normalmap.rg, sqrt(1.0-dot(normalmap.rg, normalmap.rg))); + // decode viewspace pixel position +// position.z = decodedepthmacro(dp_texture2D(Texture_ScreenDepth, ScreenTexCoord)); + position.z = normalmap.b; +// position.z = ScreenToDepth.y / (dp_texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x); + position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z); + + // now do the actual shading + // surfacenormal = pixel normal in viewspace + // LightVector = pixel to light in viewspace + // CubeVector = pixel in lightspace + // eyevector = pixel to view in viewspace + vec3 CubeVector = vec3(ViewToLight * vec4(position,1)); + myhalf fade = cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#ifdef USEDIFFUSE + // calculate diffuse shading + myhalf3 lightnormal = cast_myhalf3(normalize(LightPosition - position)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +#endif +#ifdef USESPECULAR + // calculate directional shading + vec3 eyevector = position * -1.0; +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), 1.0 + SpecularPower * normalmap.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * normalmap.a); +# endif +#endif + +#if defined(USESHADOWMAP2D) + fade *= ShadowMapCompare(CubeVector); +#endif + +#ifdef USESPECULAR + gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); + gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0); +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragData[0].rgb *= cubecolor; + gl_FragData[1].rgb *= cubecolor; +# endif +#else +# ifdef USEDIFFUSE + gl_FragColor = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0); +# else + gl_FragColor = vec4(DeferredColor_Ambient * fade, 1.0); +# endif +# ifdef USECUBEFILTER + vec3 cubecolor = dp_textureCube(Texture_Cube, CubeVector).rgb; + gl_FragColor.rgb *= cubecolor; +# endif +#endif +} +#endif // FRAGMENT_SHADER +#else // !MODE_DEFERREDLIGHTSOURCE + + + + +#ifdef VERTEX_SHADER +uniform highp mat4 TexMatrix; +#ifdef USEVERTEXTEXTUREBLEND +uniform highp mat4 BackgroundTexMatrix; +#endif +#ifdef MODE_LIGHTSOURCE +uniform highp mat4 ModelToLight; +#endif +#ifdef USESHADOWMAPORTHO +uniform highp mat4 ShadowMapMatrix; +#endif +#ifdef USEBOUNCEGRID +uniform highp mat4 BounceGridMatrix; +#endif +void main(void) +{ +#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX) + VertexColor = Attrib_Color; +#endif + // copy the surface texcoord +#ifdef USELIGHTMAP + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, Attrib_TexCoord4.xy); +#else + TexCoordSurfaceLightmap = vec4((TexMatrix * Attrib_TexCoord0).xy, 0.0, 0.0); +#endif +#ifdef USEVERTEXTEXTUREBLEND + TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0); +#endif + +#ifdef USEBOUNCEGRID + BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position); +#ifdef USEBOUNCEGRIDDIRECTIONAL + BounceGridTexCoord.z *= 0.125; +#endif +#endif + +#ifdef MODE_LIGHTSOURCE + // transform vertex position into light attenuation/cubemap space + // (-1 to +1 across the light box) + CubeVector = vec3(ModelToLight * Attrib_Position); + +# ifdef USEDIFFUSE + // transform unnormalized light direction into tangent space + // (we use unnormalized to ensure that it interpolates correctly and then + // normalize it per pixel) + vec3 lightminusvertex = LightPosition - Attrib_Position.xyz; + LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz); + LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz); + LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz); +# endif +#endif + +#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) + LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz); + LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz); + LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz); +#endif + + // transform unnormalized eye direction into tangent space +#ifdef USEEYEVECTOR + vec3 EyeRelative = EyePosition - Attrib_Position.xyz; + EyeVectorFogDepth.x = dot(EyeRelative, Attrib_TexCoord1.xyz); + EyeVectorFogDepth.y = dot(EyeRelative, Attrib_TexCoord2.xyz); + EyeVectorFogDepth.z = dot(EyeRelative, Attrib_TexCoord3.xyz); +#ifdef USEFOG + EyeVectorFogDepth.w = dot(FogPlane, Attrib_Position); +#else + EyeVectorFogDepth.w = 0.0; +#endif +#endif + + +#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE) || defined(USEBOUNCEGRIDDIRECTIONAL) +# ifdef USEFOG + VectorS = vec4(Attrib_TexCoord1.xyz, EyePosition.x - Attrib_Position.x); + VectorT = vec4(Attrib_TexCoord2.xyz, EyePosition.y - Attrib_Position.y); + VectorR = vec4(Attrib_TexCoord3.xyz, EyePosition.z - Attrib_Position.z); +# else + VectorS = vec4(Attrib_TexCoord1, 0); + VectorT = vec4(Attrib_TexCoord2, 0); + VectorR = vec4(Attrib_TexCoord3, 0); +# endif +#else +# ifdef USEFOG + EyeVectorModelSpace = EyePosition - Attrib_Position.xyz; +# endif +#endif + + // transform vertex to clipspace (post-projection, but before perspective divide by W occurs) + gl_Position = ModelViewProjectionMatrix * Attrib_Position; + +#ifdef USESHADOWMAPORTHO + ShadowMapTC = vec3(ShadowMapMatrix * gl_Position); +#endif + +#ifdef USEREFLECTION + ModelViewProjectionPosition = gl_Position; +#endif +#ifdef USETRIPPY + gl_Position = TrippyVertex(gl_Position); +#endif +} +#endif // VERTEX_SHADER + + + + +#ifdef FRAGMENT_SHADER +#ifdef USEDEFERREDLIGHTMAP +uniform myhalf2 PixelToScreenTexCoord; +uniform myhalf3 DeferredMod_Diffuse; +uniform myhalf3 DeferredMod_Specular; +#endif +uniform myhalf3 Color_Ambient; +uniform myhalf3 Color_Diffuse; +uniform myhalf3 Color_Specular; +uniform myhalf SpecularPower; +#ifdef USEGLOW +uniform myhalf3 Color_Glow; +#endif +uniform myhalf Alpha; +#ifdef USEREFLECTION +uniform mediump vec4 DistortScaleRefractReflect; +uniform mediump vec4 ScreenScaleRefractReflect; +uniform mediump vec4 ScreenCenterRefractReflect; +uniform mediump vec4 ReflectColor; +#endif +#ifdef USEREFLECTCUBE +uniform highp mat4 ModelToReflectCube; +uniform sampler2D Texture_ReflectMask; +uniform samplerCube Texture_ReflectCube; +#endif +#ifdef MODE_LIGHTDIRECTION +uniform myhalf3 LightColor; +#endif +#ifdef MODE_LIGHTSOURCE +uniform myhalf3 LightColor; +#endif +#ifdef USEBOUNCEGRID +uniform sampler3D Texture_BounceGrid; +uniform float BounceGridIntensity; +uniform highp mat4 BounceGridMatrix; +#endif +uniform highp float ClientTime; +#ifdef USENORMALMAPSCROLLBLEND +uniform highp vec2 NormalmapScrollBlend; +#endif +void main(void) +{ +#ifdef USEOFFSETMAPPING + // apply offsetmapping + vec2 dPdx = dp_offsetmapping_dFdx(TexCoordSurfaceLightmap.xy); + vec2 dPdy = dp_offsetmapping_dFdy(TexCoordSurfaceLightmap.xy); + vec2 TexCoordOffset = OffsetMapping(TexCoordSurfaceLightmap.xy, dPdx, dPdy); +# define offsetMappedTexture2D(t) dp_textureGrad(t, TexCoordOffset, dPdx, dPdy) +# define TexCoord TexCoordOffset +#else +# define offsetMappedTexture2D(t) dp_texture2D(t, TexCoordSurfaceLightmap.xy) +# define TexCoord TexCoordSurfaceLightmap.xy +#endif + + // combine the diffuse textures (base, pants, shirt) + myhalf4 color = cast_myhalf4(offsetMappedTexture2D(Texture_Color)); +#ifdef USEALPHAKILL + if (color.a < 0.5) + discard; +#endif + color.a *= Alpha; +#ifdef USECOLORMAPPING + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Pants)) * Color_Pants + cast_myhalf3(offsetMappedTexture2D(Texture_Shirt)) * Color_Shirt; +#endif +#ifdef USEVERTEXTEXTUREBLEND +#ifdef USEBOTHALPHAS + myhalf4 color2 = cast_myhalf4(dp_texture2D(Texture_SecondaryColor, TexCoord2)); + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a, cast_myhalf(1.0 - color2.a), cast_myhalf(1.0)); + color.rgb = mix(color2.rgb, color.rgb, terrainblend); +#else + myhalf terrainblend = clamp(cast_myhalf(VertexColor.a) * color.a * 2.0 - 0.5, cast_myhalf(0.0), cast_myhalf(1.0)); + //myhalf terrainblend = min(cast_myhalf(VertexColor.a) * color.a * 2.0, cast_myhalf(1.0)); + //myhalf terrainblend = cast_myhalf(VertexColor.a) * color.a > 0.5; + color.rgb = mix(cast_myhalf3(dp_texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend); +#endif + color.a = 1.0; + //color = mix(cast_myhalf4(1, 0, 0, 1), color, terrainblend); +#endif +#ifdef USEALPHAGENVERTEX + color.a *= VertexColor.a; +#endif + + // get the surface normal +#ifdef USEVERTEXTEXTUREBLEND + myhalf3 surfacenormal = normalize(mix(cast_myhalf3(dp_texture2D(Texture_SecondaryNormal, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Normal)), terrainblend) - cast_myhalf3(0.5, 0.5, 0.5)); +#else + myhalf3 surfacenormal = normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5, 0.5, 0.5)); +#endif + + // get the material colors + myhalf3 diffusetex = color.rgb; +#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP) +# ifdef USEVERTEXTEXTUREBLEND + myhalf4 glosstex = mix(cast_myhalf4(dp_texture2D(Texture_SecondaryGloss, TexCoord2)), cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)), terrainblend); +# else + myhalf4 glosstex = cast_myhalf4(offsetMappedTexture2D(Texture_Gloss)); +# endif +#endif + +#ifdef USEREFLECTCUBE + vec3 TangentReflectVector = reflect(-EyeVectorFogDepth.xyz, surfacenormal); + vec3 ModelReflectVector = TangentReflectVector.x * VectorS.xyz + TangentReflectVector.y * VectorT.xyz + TangentReflectVector.z * VectorR.xyz; + vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0)); + diffusetex += cast_myhalf3(offsetMappedTexture2D(Texture_ReflectMask)) * cast_myhalf3(dp_textureCube(Texture_ReflectCube, ReflectCubeTexCoord)); +#endif + + + + +#ifdef MODE_LIGHTSOURCE + // light source +#ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); + color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse); +#ifdef USESPECULAR +#ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +#else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +#endif + color.rgb += glosstex.rgb * (specular * Color_Specular); +#endif +#else + color.rgb = diffusetex * Color_Ambient; +#endif + color.rgb *= LightColor; + color.rgb *= cast_myhalf(dp_texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))); +#if defined(USESHADOWMAP2D) + color.rgb *= ShadowMapCompare(CubeVector); +#endif +# ifdef USECUBEFILTER + color.rgb *= cast_myhalf3(dp_textureCube(Texture_Cube, CubeVector)); +# endif +#endif // MODE_LIGHTSOURCE + + + + +#ifdef MODE_LIGHTDIRECTION + #define SHADING + #ifdef USEDIFFUSE + myhalf3 lightnormal = cast_myhalf3(normalize(LightVector)); + #endif + #define lightcolor LightColor +#endif // MODE_LIGHTDIRECTION +#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE + #define SHADING + // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe) + myhalf3 lightnormal_modelspace = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + // convert modelspace light vector to tangentspace + myhalf3 lightnormal; + lightnormal.x = dot(lightnormal_modelspace, cast_myhalf3(VectorS)); + lightnormal.y = dot(lightnormal_modelspace, cast_myhalf3(VectorT)); + lightnormal.z = dot(lightnormal_modelspace, cast_myhalf3(VectorR)); + lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this + // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division) + // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar + // is used (the lightmap and deluxemap coords correspond to virtually random coordinates + // on that luxel, and NOT to its center, because recursive triangle subdivision is used + // to map the luxels to coordinates on the draw surfaces), which also causes + // deluxemaps to be wrong because light contributions from the wrong side of the surface + // are added up. To prevent divisions by zero or strong exaggerations, a max() + // nudge is done here at expense of some additional fps. This is ONLY needed for + // deluxemaps, tangentspace deluxemap avoid this problem by design. + lightcolor *= 1.0 / max(0.25, lightnormal.z); +#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE +#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE + #define SHADING + // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light) + myhalf3 lightnormal = cast_myhalf3(dp_texture2D(Texture_Deluxemap, TexCoordSurfaceLightmap.zw)) * 2.0 + cast_myhalf3(-1.0, -1.0, -1.0); + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); +#endif +#if defined(MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) + #define SHADING + // forced deluxemap on lightmapped/vertexlit surfaces + myhalf3 lightnormal = cast_myhalf3(0.0, 0.0, 1.0); + #ifdef USELIGHTMAP + myhalf3 lightcolor = cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)); + #else + myhalf3 lightcolor = cast_myhalf3(VertexColor.rgb); + #endif +#endif +#ifdef MODE_FAKELIGHT + #define SHADING + myhalf3 lightnormal = cast_myhalf3(normalize(EyeVectorFogDepth.xyz)); + myhalf3 lightcolor = cast_myhalf3(1.0); +#endif // MODE_FAKELIGHT + + + + +#ifdef MODE_LIGHTMAP + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(dp_texture2D(Texture_Lightmap, TexCoordSurfaceLightmap.zw)) * Color_Diffuse); +#endif // MODE_LIGHTMAP +#ifdef MODE_VERTEXCOLOR + color.rgb = diffusetex * (Color_Ambient + cast_myhalf3(VertexColor.rgb) * Color_Diffuse); +#endif // MODE_VERTEXCOLOR +#ifdef MODE_FLATCOLOR + color.rgb = diffusetex * Color_Ambient; +#endif // MODE_FLATCOLOR + + + + +#ifdef SHADING +# ifdef USEDIFFUSE + myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0)); +# ifdef USESPECULAR +# ifdef USEEXACTSPECULARMATH + myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), 1.0 + SpecularPower * glosstex.a); +# else + myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz))); + myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + SpecularPower * glosstex.a); +# endif + color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor; +# else + color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor); +# endif +# else + color.rgb = diffusetex * Color_Ambient; +# endif +#endif + +#ifdef USESHADOWMAPORTHO + color.rgb *= ShadowMapCompare(ShadowMapTC); +#endif + +#ifdef USEDEFERREDLIGHTMAP + vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord; + color.rgb += diffusetex * cast_myhalf3(dp_texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse; + color.rgb += glosstex.rgb * cast_myhalf3(dp_texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular; +// color.rgb = dp_texture2D(Texture_ScreenNormalMap, ScreenTexCoord).rgb * vec3(1.0, 1.0, 0.001); +#endif + +#ifdef USEBOUNCEGRID +#ifdef USEBOUNCEGRIDDIRECTIONAL +// myhalf4 bouncegrid_coeff1 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord )); +// myhalf4 bouncegrid_coeff2 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.125))) * 2.0 + cast_myhalf4(-1.0, -1.0, -1.0, -1.0); + myhalf4 bouncegrid_coeff3 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.250))); + myhalf4 bouncegrid_coeff4 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.375))); + myhalf4 bouncegrid_coeff5 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.500))); + myhalf4 bouncegrid_coeff6 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.625))); + myhalf4 bouncegrid_coeff7 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.750))); + myhalf4 bouncegrid_coeff8 = cast_myhalf4(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord + vec3(0.0, 0.0, 0.875))); + myhalf3 bouncegrid_dir = normalize(mat3(BounceGridMatrix) * (surfacenormal.x * VectorS.xyz + surfacenormal.y * VectorT.xyz + surfacenormal.z * VectorR.xyz)); + myhalf3 bouncegrid_dirp = max(cast_myhalf3(0.0, 0.0, 0.0), bouncegrid_dir); + myhalf3 bouncegrid_dirn = max(cast_myhalf3(0.0, 0.0, 0.0), -bouncegrid_dir); +// bouncegrid_dirp = bouncegrid_dirn = cast_myhalf3(1.0,1.0,1.0); + myhalf3 bouncegrid_light = cast_myhalf3( + dot(bouncegrid_coeff3.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff6.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff4.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff7.xyz, bouncegrid_dirn), + dot(bouncegrid_coeff5.xyz, bouncegrid_dirp) + dot(bouncegrid_coeff8.xyz, bouncegrid_dirn)); + color.rgb += diffusetex * bouncegrid_light * BounceGridIntensity; +// color.rgb = bouncegrid_dir.rgb * 0.5 + vec3(0.5, 0.5, 0.5); +#else + color.rgb += diffusetex * cast_myhalf3(dp_texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity; +#endif +#endif + +#ifdef USEGLOW +#ifdef USEVERTEXTEXTUREBLEND + color.rgb += mix(cast_myhalf3(dp_texture2D(Texture_SecondaryGlow, TexCoord2)), cast_myhalf3(offsetMappedTexture2D(Texture_Glow)), terrainblend) * Color_Glow; +#else + color.rgb += cast_myhalf3(offsetMappedTexture2D(Texture_Glow)) * Color_Glow; +#endif +#endif + +#ifdef USEFOG + color.rgb = FogVertex(color); +#endif + + // 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 +#ifdef USEREFLECTION + vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w); + //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect; + vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw; + #ifdef USENORMALMAPSCROLLBLEND +# ifdef USEOFFSETMAPPING + vec3 normal = dp_textureGrad(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y, dPdx*NormalmapScrollBlend.y, dPdy*NormalmapScrollBlend.y).rgb - vec3(1.0); +# else + vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0); +# endif + normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb; + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.zw; + #else + vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(offsetMappedTexture2D(Texture_Normal)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.zw; + #endif + // FIXME temporary hack to detect the case that the reflection + // gets blackened at edges due to leaving the area that contains actual + // content. + // Remove this 'ack once we have a better way to stop this thing from + // 'appening. + float f = min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05); + f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05); + ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f); + color.rgb = mix(color.rgb, cast_myhalf3(dp_texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a); +#endif + + dp_FragColor = vec4(color); +} +#endif // FRAGMENT_SHADER + +#endif // !MODE_DEFERREDLIGHTSOURCE +#endif // !MODE_DEFERREDGEOMETRY +#endif // !MODE_WATER +#endif // !MODE_REFRACTION +#endif // !MODE_BLOOMBLUR +#endif // !MODE_GENERIC +#endif // !MODE_POSTPROCESS +#endif // !MODE_SHOWDEPTH +#endif // !MODE_DEPTH_OR_SHADOW + -- cgit v1.2.3