1
0
Fork 0

Change normal encoding method to get rid of RG16 textures and buffers

This commit is contained in:
Frederic Bouvier 2012-04-12 23:58:03 +02:00
parent c27a4cfebe
commit c05b12a6ab
16 changed files with 73 additions and 40 deletions

View file

@ -34,6 +34,7 @@
<program> <program>
<vertex-shader>Shaders/fog.vert</vertex-shader> <vertex-shader>Shaders/fog.vert</vertex-shader>
<fragment-shader>Shaders/fog.frag</fragment-shader> <fragment-shader>Shaders/fog.frag</fragment-shader>
<fragment-shader>Shaders/gbuffer-functions.frag</fragment-shader>
</program> </program>
<uniform> <uniform>
<name>depth_tex</name> <name>depth_tex</name>

View file

@ -36,6 +36,7 @@
<program> <program>
<vertex-shader>Shaders/light-spot.vert</vertex-shader> <vertex-shader>Shaders/light-spot.vert</vertex-shader>
<fragment-shader>Shaders/light-point.frag</fragment-shader> <fragment-shader>Shaders/light-point.frag</fragment-shader>
<fragment-shader>Shaders/gbuffer-functions.frag</fragment-shader>
<attribute> <attribute>
<name>attenuation</name> <name>attenuation</name>
<index>12</index> <index>12</index>

View file

@ -36,6 +36,7 @@
<program> <program>
<vertex-shader>Shaders/light-spot.vert</vertex-shader> <vertex-shader>Shaders/light-spot.vert</vertex-shader>
<fragment-shader>Shaders/light-spot.frag</fragment-shader> <fragment-shader>Shaders/light-spot.frag</fragment-shader>
<fragment-shader>Shaders/gbuffer-functions.frag</fragment-shader>
<attribute> <attribute>
<name>attenuation</name> <name>attenuation</name>
<index>12</index> <index>12</index>

View file

@ -26,7 +26,6 @@ the objects that use it, and replaces it with the default shader.
</and> </and>
</or> </or>
<extension-supported>GL_EXT_gpu_shader4</extension-supported> <extension-supported>GL_EXT_gpu_shader4</extension-supported>
<extension-supported>GL_ARB_texture_rg</extension-supported>
</and> </and>
</predicate> </predicate>
<pass> <pass>
@ -51,8 +50,9 @@ the objects that use it, and replaces it with the default shader.
</material> </material>
<cull-face>back</cull-face> <cull-face>back</cull-face>
<program> <program>
<vertex-shader n="1">Shaders/ubershader.vert</vertex-shader> <vertex-shader>Shaders/ubershader.vert</vertex-shader>
<fragment-shader n="1">Shaders/ubershader-gbuffer.frag</fragment-shader> <fragment-shader>Shaders/ubershader-gbuffer.frag</fragment-shader>
<fragment-shader>Shaders/gbuffer-functions.frag</fragment-shader>
<!-- <vertex-shader n="1">Shaders/deferred-gbuffer.vert</vertex-shader> <!-- <vertex-shader n="1">Shaders/deferred-gbuffer.vert</vertex-shader>
<fragment-shader n="1">Shaders/deferred-gbuffer.frag</fragment-shader>--> <fragment-shader n="1">Shaders/deferred-gbuffer.frag</fragment-shader>-->
</program> </program>

View file

@ -149,7 +149,6 @@
</and> </and>
</or> </or>
<extension-supported>GL_EXT_gpu_shader4</extension-supported> <extension-supported>GL_EXT_gpu_shader4</extension-supported>
<extension-supported>GL_ARB_texture_rg</extension-supported>
</and> </and>
</predicate> </predicate>
<pass> <pass>
@ -189,6 +188,7 @@
<program> <program>
<vertex-shader>Shaders/deferred-gbuffer.vert</vertex-shader> <vertex-shader>Shaders/deferred-gbuffer.vert</vertex-shader>
<fragment-shader>Shaders/deferred-gbuffer.frag</fragment-shader> <fragment-shader>Shaders/deferred-gbuffer.frag</fragment-shader>
<fragment-shader>Shaders/gbuffer-functions.frag</fragment-shader>
</program> </program>
<uniform> <uniform>
<name>texture</name> <name>texture</name>

View file

@ -41,6 +41,7 @@
<program> <program>
<vertex-shader>Shaders/sunlight.vert</vertex-shader> <vertex-shader>Shaders/sunlight.vert</vertex-shader>
<fragment-shader>Shaders/sunlight.frag</fragment-shader> <fragment-shader>Shaders/sunlight.frag</fragment-shader>
<fragment-shader>Shaders/gbuffer-functions.frag</fragment-shader>
</program> </program>
<uniform> <uniform>
<name>depth_tex</name> <name>depth_tex</name>

View file

@ -212,7 +212,6 @@
</and> </and>
</or> </or>
<extension-supported>GL_EXT_gpu_shader4</extension-supported> <extension-supported>GL_EXT_gpu_shader4</extension-supported>
<extension-supported>GL_ARB_texture_rg</extension-supported>
</and> </and>
</predicate> </predicate>
<pass> <pass>
@ -249,6 +248,7 @@
<program> <program>
<vertex-shader>Shaders/deferred-gbuffer.vert</vertex-shader> <vertex-shader>Shaders/deferred-gbuffer.vert</vertex-shader>
<fragment-shader>Shaders/deferred-gbuffer.frag</fragment-shader> <fragment-shader>Shaders/deferred-gbuffer.frag</fragment-shader>
<fragment-shader>Shaders/gbuffer-functions.frag</fragment-shader>
</program> </program>
<uniform> <uniform>
<name>texture</name> <name>texture</name>

View file

@ -152,8 +152,9 @@
<type>noise</type> <type>noise</type>
</texture-unit> </texture-unit>
<program> <program>
<vertex-shader n="1">Shaders/urban-gbuffer.vert</vertex-shader> <vertex-shader>Shaders/urban-gbuffer.vert</vertex-shader>
<fragment-shader n="1">Shaders/urban-gbuffer.frag</fragment-shader> <fragment-shader>Shaders/urban-gbuffer.frag</fragment-shader>
<fragment-shader>Shaders/gbuffer-functions.frag</fragment-shader>
<attribute> <attribute>
<name>tangent</name> <name>tangent</name>
<index>6</index> <index>6</index>
@ -313,6 +314,7 @@
<program> <program>
<vertex-shader>Shaders/urban-gbuffer.vert</vertex-shader> <vertex-shader>Shaders/urban-gbuffer.vert</vertex-shader>
<fragment-shader>Shaders/urban-gbuffer.frag</fragment-shader> <fragment-shader>Shaders/urban-gbuffer.frag</fragment-shader>
<fragment-shader>Shaders/gbuffer-functions.frag</fragment-shader>
<attribute> <attribute>
<name>tangent</name> <name>tangent</name>
<index>6</index> <index>6</index>

View file

@ -8,6 +8,9 @@ varying vec3 ecNormal;
varying float alpha; varying float alpha;
uniform int materialID; uniform int materialID;
uniform sampler2D texture; uniform sampler2D texture;
vec2 normal_encode(vec3 n);
void main() { void main() {
vec4 texel = texture2D(texture, gl_TexCoord[0].st); vec4 texel = texture2D(texture, gl_TexCoord[0].st);
if (texel.a * alpha < 0.1) 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 ) ); 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 ); 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[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 );
} }

View file

@ -6,6 +6,9 @@ uniform vec4 fg_FogColor;
uniform float fg_FogDensity; uniform float fg_FogDensity;
uniform vec3 fg_Planes; uniform vec3 fg_Planes;
varying vec3 ray; varying vec3 ray;
vec3 position( vec3 viewdir, float depth );
void main() { void main() {
vec2 coords = gl_TexCoord[0].xy; vec2 coords = gl_TexCoord[0].xy;
float initialized = texture2D( spec_emis_tex, coords ).a; float initialized = texture2D( spec_emis_tex, coords ).a;
@ -16,11 +19,7 @@ void main() {
normal.z = sqrt( 1.0 - dot( normal.xy, normal.xy ) ); normal.z = sqrt( 1.0 - dot( normal.xy, normal.xy ) );
float len = length(normal); float len = length(normal);
normal /= len; normal /= len;
vec3 viewDir = normalize(ray); vec3 pos = position( normalize(ray), texture2D( depth_tex, coords ).r );
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;
float fogFactor = 0.0; float fogFactor = 0.0;
const float LOG2 = 1.442695; const float LOG2 = 1.442695;

View file

@ -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;
}

View file

@ -15,21 +15,19 @@ uniform float Far;
varying vec4 ecPosition; varying vec4 ecPosition;
vec3 position( vec3 viewdir, float depth );
vec3 normal_decode(vec2 enc);
void main() { void main() {
vec3 ray = ecPosition.xyz / ecPosition.w; vec3 ray = ecPosition.xyz / ecPosition.w;
vec3 ecPos3 = ray; vec3 ecPos3 = ray;
vec3 viewDir = normalize(ray); vec3 viewDir = normalize(ray);
vec2 coords = gl_FragCoord.xy / fg_BufferSize; vec2 coords = gl_FragCoord.xy / fg_BufferSize;
float depth = texture2D( depth_tex, coords ).r; vec3 normal = normal_decode(texture2D( normal_tex, coords ).rg);
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 ) );
vec4 spec_emis = texture2D( spec_emis_tex, coords ); vec4 spec_emis = texture2D( spec_emis_tex, coords );
vec3 pos; vec3 pos = position(viewDir, texture2D( depth_tex, coords ).r);
pos.z = - fg_Planes.y / (fg_Planes.x + depth * fg_Planes.z);
pos.xy = viewDir.xy * pos.z / viewDir.z;
if ( pos.z < ecPos3.z ) // Negative direction in z if ( pos.z < ecPos3.z ) // Negative direction in z
discard; // Don't light surface outside the light volume discard; // Don't light surface outside the light volume

View file

@ -19,21 +19,19 @@ uniform float Far;
varying vec4 ecPosition; varying vec4 ecPosition;
vec3 position( vec3 viewdir, float depth );
vec3 normal_decode(vec2 enc);
void main() { void main() {
vec3 ray = ecPosition.xyz / ecPosition.w; vec3 ray = ecPosition.xyz / ecPosition.w;
vec3 ecPos3 = ray; vec3 ecPos3 = ray;
vec3 viewDir = normalize(ray); vec3 viewDir = normalize(ray);
vec2 coords = gl_FragCoord.xy / fg_BufferSize; vec2 coords = gl_FragCoord.xy / fg_BufferSize;
float depth = texture2D( depth_tex, coords ).r; vec3 normal = normal_decode(texture2D( normal_tex, coords ).rg);
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 ) );
vec4 spec_emis = texture2D( spec_emis_tex, coords ); vec4 spec_emis = texture2D( spec_emis_tex, coords );
vec3 pos; vec3 pos = position(viewDir, texture2D( depth_tex, coords ).r);
pos.z = - fg_Planes.y / (fg_Planes.x + depth * fg_Planes.z);
pos.xy = viewDir.xy * pos.z / viewDir.z;
if ( pos.z < ecPos3.z ) // Negative direction in z if ( pos.z < ecPos3.z ) // Negative direction in z
discard; // Don't light surface outside the light volume discard; // Don't light surface outside the light volume

View file

@ -9,6 +9,10 @@ uniform vec4 fg_SunSpecularColor;
uniform vec3 fg_SunDirection; uniform vec3 fg_SunDirection;
uniform vec3 fg_Planes; uniform vec3 fg_Planes;
varying vec3 ray; varying vec3 ray;
vec3 position( vec3 viewdir, float depth );
vec3 normal_decode(vec2 enc);
vec4 DynamicShadow( in vec4 ecPosition, out vec4 tint ) vec4 DynamicShadow( in vec4 ecPosition, out vec4 tint )
{ {
vec4 coords; vec4 coords;
@ -44,16 +48,11 @@ void main() {
vec4 spec_emis = texture2D( spec_emis_tex, coords ); vec4 spec_emis = texture2D( spec_emis_tex, coords );
if ( spec_emis.a < 0.1 ) if ( spec_emis.a < 0.1 )
discard; discard;
vec3 normal; vec3 normal = normal_decode(texture2D( normal_tex, coords ).rg);
normal.xy = texture2D( normal_tex, coords ).rg * 2.0 - vec2(1.0,1.0);
normal.z = sqrt( 1.0 - dot( normal.xy, normal.xy ) );
float len = length(normal); float len = length(normal);
normal /= len; normal /= len;
vec3 viewDir = normalize(ray); vec3 viewDir = normalize(ray);
float depth = texture2D( depth_tex, coords ).r; vec3 pos = position( viewDir, 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;
vec4 tint; vec4 tint;
#if 0 #if 0
@ -86,7 +85,7 @@ void main() {
vec3 Iemis = spec_emis.z * color; vec3 Iemis = spec_emis.z * color;
if (len > 0.0001) { if (len > 0.0001) {
halfDir /= len; 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 = 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); // gl_FragColor = mix(tint, vec4(mix(vec3(0.0), Idiff + Ispec, shadow) + Iemis, 1.0), 0.92);

View file

@ -56,6 +56,7 @@ uniform vec3 dirt_g_color;
uniform vec3 dirt_b_color; uniform vec3 dirt_b_color;
//uniform vec4 fg_SunAmbientColor; //uniform vec4 fg_SunAmbientColor;
vec2 normal_encode(vec3 n);
///fog include////////////////////// ///fog include//////////////////////
uniform int fogType; uniform int fogType;
@ -87,7 +88,6 @@ void main (void)
} else { } else {
N = normalize(VNormal); N = normalize(VNormal);
} }
vec2 eyeN = (N.xy + vec2(1.0,1.0))*0.5;
///END bump ///END bump
vec4 reflection = textureCube(Environment, reflVec * N); vec4 reflection = textureCube(Environment, reflVec * N);
vec3 viewVec = normalize(vViewVec); vec3 viewVec = normalize(vViewVec);
@ -180,7 +180,7 @@ void main (void)
// END lightmap // 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[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);
} }

View file

@ -37,6 +37,8 @@ int linear_search_steps = 10;
int GlobalIterationCount = 0; int GlobalIterationCount = 0;
int gIterationCap = 64; int gIterationCap = 64;
vec2 normal_encode(vec3 n);
void QDM(inout vec3 p, inout vec3 v) void QDM(inout vec3 p, inout vec3 v)
{ {
const int MAX_LEVEL = TEXTURE_MIP_LEVELS; 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))); N.z = sqrt(1.0 - min(1.0,dot(N.xy, N.xy)));
float Nz = N.z; float Nz = N.z;
N = normalize(N.x * VTangent + N.y * VBinormal + N.z * VNormal); 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); vec4 ambient_light = constantColor + vec4(gl_Color.rgb, 1.0);