diff --git a/Effects/fog.eff b/Effects/fog.eff
index 03cd260c5..1092fc908 100644
--- a/Effects/fog.eff
+++ b/Effects/fog.eff
@@ -34,6 +34,7 @@
Shaders/fog.vert
Shaders/fog.frag
+ Shaders/gbuffer-functions.frag
depth_tex
diff --git a/Effects/light-point.eff b/Effects/light-point.eff
index 332925f61..34d5d027f 100644
--- a/Effects/light-point.eff
+++ b/Effects/light-point.eff
@@ -36,6 +36,7 @@
Shaders/light-spot.vert
Shaders/light-point.frag
+ Shaders/gbuffer-functions.frag
attenuation
12
diff --git a/Effects/light-spot.eff b/Effects/light-spot.eff
index b2501f980..210ac4323 100644
--- a/Effects/light-spot.eff
+++ b/Effects/light-spot.eff
@@ -36,6 +36,7 @@
Shaders/light-spot.vert
Shaders/light-spot.frag
+ Shaders/gbuffer-functions.frag
attenuation
12
diff --git a/Effects/model-combined-deferred.eff b/Effects/model-combined-deferred.eff
index afe26e7e8..3897332e2 100644
--- a/Effects/model-combined-deferred.eff
+++ b/Effects/model-combined-deferred.eff
@@ -26,7 +26,6 @@ the objects that use it, and replaces it with the default shader.
GL_EXT_gpu_shader4
- GL_ARB_texture_rg
@@ -51,8 +50,9 @@ the objects that use it, and replaces it with the default shader.
back
- Shaders/ubershader.vert
- Shaders/ubershader-gbuffer.frag
+ Shaders/ubershader.vert
+ Shaders/ubershader-gbuffer.frag
+ Shaders/gbuffer-functions.frag
diff --git a/Effects/model-default.eff b/Effects/model-default.eff
index d5185e98f..08366e17b 100644
--- a/Effects/model-default.eff
+++ b/Effects/model-default.eff
@@ -149,7 +149,6 @@
GL_EXT_gpu_shader4
- GL_ARB_texture_rg
@@ -189,6 +188,7 @@
Shaders/deferred-gbuffer.vert
Shaders/deferred-gbuffer.frag
+ Shaders/gbuffer-functions.frag
texture
diff --git a/Effects/sunlight.eff b/Effects/sunlight.eff
index b685049ed..9905237ca 100644
--- a/Effects/sunlight.eff
+++ b/Effects/sunlight.eff
@@ -41,6 +41,7 @@
Shaders/sunlight.vert
Shaders/sunlight.frag
+ Shaders/gbuffer-functions.frag
depth_tex
diff --git a/Effects/terrain-default.eff b/Effects/terrain-default.eff
index 0f09216cf..f550dcedf 100644
--- a/Effects/terrain-default.eff
+++ b/Effects/terrain-default.eff
@@ -212,7 +212,6 @@
GL_EXT_gpu_shader4
- GL_ARB_texture_rg
@@ -249,6 +248,7 @@
Shaders/deferred-gbuffer.vert
Shaders/deferred-gbuffer.frag
+ Shaders/gbuffer-functions.frag
texture
diff --git a/Effects/urban.eff b/Effects/urban.eff
index a214a112b..4c3473c1f 100644
--- a/Effects/urban.eff
+++ b/Effects/urban.eff
@@ -152,8 +152,9 @@
noise
- Shaders/urban-gbuffer.vert
- Shaders/urban-gbuffer.frag
+ Shaders/urban-gbuffer.vert
+ Shaders/urban-gbuffer.frag
+ Shaders/gbuffer-functions.frag
tangent
6
@@ -313,6 +314,7 @@
Shaders/urban-gbuffer.vert
Shaders/urban-gbuffer.frag
+ Shaders/gbuffer-functions.frag
tangent
6
diff --git a/Shaders/deferred-gbuffer.frag b/Shaders/deferred-gbuffer.frag
index 76b8d34dc..e1d3b4cf9 100644
--- a/Shaders/deferred-gbuffer.frag
+++ b/Shaders/deferred-gbuffer.frag
@@ -8,6 +8,9 @@ varying vec3 ecNormal;
varying float alpha;
uniform int materialID;
uniform sampler2D texture;
+
+vec2 normal_encode(vec3 n);
+
void main() {
vec4 texel = texture2D(texture, gl_TexCoord[0].st);
if (texel.a * alpha < 0.1)
@@ -17,7 +20,7 @@ void main() {
float emission = dot( gl_FrontLightModelProduct.sceneColor.rgb, vec3( 0.3, 0.59, 0.11 ) );
vec3 normal2 = normalize( (2.0 * gl_Color.a - 1.0) * ecNormal );
- gl_FragData[0] = vec4( (normal2.xy + vec2(1.0,1.0)) * 0.5, 0.0, 1.0 );
+ gl_FragData[0] = vec4( normal_encode(normal2), 0.0, 1.0 );
gl_FragData[1] = vec4( gl_Color.rgb * texel.rgb, float( materialID ) / 255.0 );
- gl_FragData[2] = vec4( specular, shininess / 255.0, emission, 1.0 );
+ gl_FragData[2] = vec4( specular, shininess / 128.0, emission, 1.0 );
}
diff --git a/Shaders/fog.frag b/Shaders/fog.frag
index b61b612a5..09280db14 100644
--- a/Shaders/fog.frag
+++ b/Shaders/fog.frag
@@ -6,6 +6,9 @@ uniform vec4 fg_FogColor;
uniform float fg_FogDensity;
uniform vec3 fg_Planes;
varying vec3 ray;
+
+vec3 position( vec3 viewdir, float depth );
+
void main() {
vec2 coords = gl_TexCoord[0].xy;
float initialized = texture2D( spec_emis_tex, coords ).a;
@@ -16,11 +19,7 @@ void main() {
normal.z = sqrt( 1.0 - dot( normal.xy, normal.xy ) );
float len = length(normal);
normal /= len;
- vec3 viewDir = normalize(ray);
- float depth = texture2D( depth_tex, coords ).r;
- vec3 pos;
- pos.z = - fg_Planes.y / (fg_Planes.x + depth * fg_Planes.z);
- pos.xy = viewDir.xy / viewDir.z * pos.z;
+ vec3 pos = position( normalize(ray), texture2D( depth_tex, coords ).r );
float fogFactor = 0.0;
const float LOG2 = 1.442695;
diff --git a/Shaders/gbuffer-functions.frag b/Shaders/gbuffer-functions.frag
new file mode 100644
index 000000000..5b8addee6
--- /dev/null
+++ b/Shaders/gbuffer-functions.frag
@@ -0,0 +1,28 @@
+uniform vec3 fg_Planes;
+
+// normal compression functions from
+// http://aras-p.info/texts/CompactNormalStorage.html#method04spheremap
+vec2 normal_encode(vec3 n)
+{
+ float p = sqrt(n.z * 8.0 + 8.0);
+ return n.xy / p + 0.5;
+}
+
+vec3 normal_decode(vec2 enc)
+{
+ vec2 fenc = enc * 4.0 - 2.0;
+ float f = dot(fenc,fenc);
+ float g = sqrt(1.0 - f / 4.0);
+ vec3 n;
+ n.xy = fenc * g;
+ n.z = 1.0 - f / 2.0;
+ return n;
+}
+
+vec3 position( vec3 viewDir, float depth )
+{
+ vec3 pos;
+ pos.z = - fg_Planes.y / (fg_Planes.x + depth * fg_Planes.z);
+ pos.xy = viewDir.xy / viewDir.z * pos.z;
+ return pos;
+}
diff --git a/Shaders/light-point.frag b/Shaders/light-point.frag
index f02b684e9..f71a40600 100644
--- a/Shaders/light-point.frag
+++ b/Shaders/light-point.frag
@@ -15,21 +15,19 @@ uniform float Far;
varying vec4 ecPosition;
+vec3 position( vec3 viewdir, float depth );
+vec3 normal_decode(vec2 enc);
+
void main() {
vec3 ray = ecPosition.xyz / ecPosition.w;
vec3 ecPos3 = ray;
vec3 viewDir = normalize(ray);
vec2 coords = gl_FragCoord.xy / fg_BufferSize;
- float depth = texture2D( depth_tex, coords ).r;
- vec3 normal;
- normal.xy = texture2D( normal_tex, coords ).rg * 2.0 - vec2(1.0,1.0);
- normal.z = sqrt( 1.0 - dot( normal.xy, normal.xy ) );
+ vec3 normal = normal_decode(texture2D( normal_tex, coords ).rg);
vec4 spec_emis = texture2D( spec_emis_tex, coords );
- vec3 pos;
- pos.z = - fg_Planes.y / (fg_Planes.x + depth * fg_Planes.z);
- pos.xy = viewDir.xy * pos.z / viewDir.z;
+ vec3 pos = position(viewDir, texture2D( depth_tex, coords ).r);
if ( pos.z < ecPos3.z ) // Negative direction in z
discard; // Don't light surface outside the light volume
diff --git a/Shaders/light-spot.frag b/Shaders/light-spot.frag
index 5e6285f2b..7371eec6f 100644
--- a/Shaders/light-spot.frag
+++ b/Shaders/light-spot.frag
@@ -19,21 +19,19 @@ uniform float Far;
varying vec4 ecPosition;
+vec3 position( vec3 viewdir, float depth );
+vec3 normal_decode(vec2 enc);
+
void main() {
vec3 ray = ecPosition.xyz / ecPosition.w;
vec3 ecPos3 = ray;
vec3 viewDir = normalize(ray);
vec2 coords = gl_FragCoord.xy / fg_BufferSize;
- float depth = texture2D( depth_tex, coords ).r;
- vec3 normal;
- normal.xy = texture2D( normal_tex, coords ).rg * 2.0 - vec2(1.0,1.0);
- normal.z = sqrt( 1.0 - dot( normal.xy, normal.xy ) );
+ vec3 normal = normal_decode(texture2D( normal_tex, coords ).rg);
vec4 spec_emis = texture2D( spec_emis_tex, coords );
- vec3 pos;
- pos.z = - fg_Planes.y / (fg_Planes.x + depth * fg_Planes.z);
- pos.xy = viewDir.xy * pos.z / viewDir.z;
+ vec3 pos = position(viewDir, texture2D( depth_tex, coords ).r);
if ( pos.z < ecPos3.z ) // Negative direction in z
discard; // Don't light surface outside the light volume
diff --git a/Shaders/sunlight.frag b/Shaders/sunlight.frag
index 81f0c12f7..4c616819c 100644
--- a/Shaders/sunlight.frag
+++ b/Shaders/sunlight.frag
@@ -9,6 +9,10 @@ uniform vec4 fg_SunSpecularColor;
uniform vec3 fg_SunDirection;
uniform vec3 fg_Planes;
varying vec3 ray;
+
+vec3 position( vec3 viewdir, float depth );
+vec3 normal_decode(vec2 enc);
+
vec4 DynamicShadow( in vec4 ecPosition, out vec4 tint )
{
vec4 coords;
@@ -44,16 +48,11 @@ void main() {
vec4 spec_emis = texture2D( spec_emis_tex, coords );
if ( spec_emis.a < 0.1 )
discard;
- vec3 normal;
- normal.xy = texture2D( normal_tex, coords ).rg * 2.0 - vec2(1.0,1.0);
- normal.z = sqrt( 1.0 - dot( normal.xy, normal.xy ) );
+ vec3 normal = normal_decode(texture2D( normal_tex, coords ).rg);
float len = length(normal);
normal /= len;
vec3 viewDir = normalize(ray);
- float depth = texture2D( depth_tex, coords ).r;
- vec3 pos;
- pos.z = - fg_Planes.y / (fg_Planes.x + depth * fg_Planes.z);
- pos.xy = viewDir.xy / viewDir.z * pos.z;
+ vec3 pos = position( viewDir, texture2D( depth_tex, coords ).r );
vec4 tint;
#if 0
@@ -86,7 +85,7 @@ void main() {
vec3 Iemis = spec_emis.z * color;
if (len > 0.0001) {
halfDir /= len;
- Ispec = pow( clamp( dot( halfDir, normal ), 0.0, 1.0 ), spec_emis.y * 255.0 ) * spec_emis.x * fg_SunSpecularColor.rgb;
+ Ispec = pow( clamp( dot( halfDir, normal ), 0.0, 1.0 ), spec_emis.y * 128.0 ) * spec_emis.x * fg_SunSpecularColor.rgb;
}
gl_FragColor = vec4(mix(vec3(0.0), Idiff + Ispec, shadow) + Iemis, 1.0);
// gl_FragColor = mix(tint, vec4(mix(vec3(0.0), Idiff + Ispec, shadow) + Iemis, 1.0), 0.92);
diff --git a/Shaders/ubershader-gbuffer.frag b/Shaders/ubershader-gbuffer.frag
index e7f6d9655..ff6c48a3a 100644
--- a/Shaders/ubershader-gbuffer.frag
+++ b/Shaders/ubershader-gbuffer.frag
@@ -56,6 +56,7 @@ uniform vec3 dirt_g_color;
uniform vec3 dirt_b_color;
//uniform vec4 fg_SunAmbientColor;
+vec2 normal_encode(vec3 n);
///fog include//////////////////////
uniform int fogType;
@@ -87,7 +88,6 @@ void main (void)
} else {
N = normalize(VNormal);
}
- vec2 eyeN = (N.xy + vec2(1.0,1.0))*0.5;
///END bump
vec4 reflection = textureCube(Environment, reflVec * N);
vec3 viewVec = normalize(vViewVec);
@@ -180,7 +180,7 @@ void main (void)
// END lightmap
/////////////////////////////////////////////////////////////////////
- gl_FragData[0]=vec4(eyeN, 0.0, 1.0);
+ gl_FragData[0]=vec4(normal_encode(N), 0.0, 1.0);
gl_FragData[1]=vec4(fragColor.rgb,1.0/255.0);
- gl_FragData[2]=vec4(specular, gl_FrontMaterial.shininess/255.0, emission, 1.0);
+ gl_FragData[2]=vec4(specular, gl_FrontMaterial.shininess/128.0, emission, 1.0);
}
\ No newline at end of file
diff --git a/Shaders/urban-gbuffer.frag b/Shaders/urban-gbuffer.frag
index a3349d8a4..ed6b01219 100644
--- a/Shaders/urban-gbuffer.frag
+++ b/Shaders/urban-gbuffer.frag
@@ -37,6 +37,8 @@ int linear_search_steps = 10;
int GlobalIterationCount = 0;
int gIterationCap = 64;
+vec2 normal_encode(vec3 n);
+
void QDM(inout vec3 p, inout vec3 v)
{
const int MAX_LEVEL = TEXTURE_MIP_LEVELS;
@@ -184,7 +186,7 @@ void main (void)
N.z = sqrt(1.0 - min(1.0,dot(N.xy, N.xy)));
float Nz = N.z;
N = normalize(N.x * VTangent + N.y * VBinormal + N.z * VNormal);
- gl_FragData[0] = vec4( (N.xy + vec2(1.0,1.0)) * 0.5, 0.0, 1.0 );
+ gl_FragData[0] = vec4( normal_encode(N), 0.0, 1.0 );
vec4 ambient_light = constantColor + vec4(gl_Color.rgb, 1.0);