Skip to content
Snippets Groups Projects
Commit 514186ee authored by Tofu Linden's avatar Tofu Linden
Browse files

Rejig deferred normal-map packing a little, to double its accuracy for free.

(transplanted from bf541483d9074c18c6d4f4bd19caf0de82013cbf)
parent 6b26a7c3
No related branches found
No related tags found
No related merge requests found
Showing
with 53 additions and 27 deletions
...@@ -20,6 +20,7 @@ void main() ...@@ -20,6 +20,7 @@ void main()
gl_FragData[0] = vec4(diff.rgb, 0.0); gl_FragData[0] = vec4(diff.rgb, 0.0);
gl_FragData[1] = vec4(0,0,0,0); gl_FragData[1] = vec4(0,0,0,0);
gl_FragData[2] = vec4(normalize(vary_normal)*0.5+0.5, 0.0); vec3 nvn = normalize(vary_normal);
gl_FragData[2] = vec4(nvn.xy * 0.5 + 0.5, nvn.z, 0.0);
} }
...@@ -37,7 +37,8 @@ vec4 getPosition(vec2 pos_screen) ...@@ -37,7 +37,8 @@ vec4 getPosition(vec2 pos_screen)
void main() void main()
{ {
vec3 norm = texture2DRect(normalMap, vary_fragcoord.xy).xyz*2.0-1.0; vec3 norm = texture2DRect(normalMap, vary_fragcoord.xy).xyz;
norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
vec3 pos = getPosition(vary_fragcoord.xy).xyz; vec3 pos = getPosition(vary_fragcoord.xy).xyz;
vec4 ccol = texture2DRect(lightMap, vary_fragcoord.xy).rgba; vec4 ccol = texture2DRect(lightMap, vary_fragcoord.xy).rgba;
......
...@@ -24,5 +24,6 @@ void main() ...@@ -24,5 +24,6 @@ void main()
gl_FragData[0] = vec4(col, 0.0); gl_FragData[0] = vec4(col, 0.0);
gl_FragData[1] = gl_Color.aaaa; // spec gl_FragData[1] = gl_Color.aaaa; // spec
//gl_FragData[1] = vec4(vec3(gl_Color.a), gl_Color.a+(1.0-gl_Color.a)*gl_Color.a); // spec - from former class3 - maybe better, but not so well tested //gl_FragData[1] = vec4(vec3(gl_Color.a), gl_Color.a+(1.0-gl_Color.a)*gl_Color.a); // spec - from former class3 - maybe better, but not so well tested
gl_FragData[2] = vec4(normalize(tnorm)*0.5+0.5, 0.0); vec3 nvn = normalize(tnorm);
gl_FragData[2] = vec4(nvn.xy * 0.5 + 0.5, nvn.z, 0.0);
} }
...@@ -15,5 +15,6 @@ void main() ...@@ -15,5 +15,6 @@ void main()
gl_FragData[0] = vec4(col, 0.0); gl_FragData[0] = vec4(col, 0.0);
gl_FragData[1] = gl_Color.aaaa; // spec gl_FragData[1] = gl_Color.aaaa; // spec
//gl_FragData[1] = vec4(vec3(gl_Color.a), gl_Color.a+(1.0-gl_Color.a)*gl_Color.a); // spec - from former class3 - maybe better, but not so well tested //gl_FragData[1] = vec4(vec3(gl_Color.a), gl_Color.a+(1.0-gl_Color.a)*gl_Color.a); // spec - from former class3 - maybe better, but not so well tested
gl_FragData[2] = vec4(normalize(vary_normal)*0.5+0.5, 0.0); vec3 nvn = normalize(vary_normal);
gl_FragData[2] = vec4(nvn.xy * 0.5 + 0.5, nvn.z, 0.0);
} }
...@@ -159,7 +159,8 @@ void main() ...@@ -159,7 +159,8 @@ void main()
{ {
vec2 pos_screen = vary_fragcoord.xy; vec2 pos_screen = vary_fragcoord.xy;
vec4 pos = getPosition(pos_screen); vec4 pos = getPosition(pos_screen);
vec3 norm = texture2DRect(normalMap, pos_screen).xyz*2.0-1.0; vec3 norm = texture2DRect(normalMap, pos_screen).xyz;
norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
gl_FragData[0].xyz = giAmbient(pos, norm); gl_FragData[0].xyz = giAmbient(pos, norm);
} }
...@@ -53,7 +53,8 @@ void main() ...@@ -53,7 +53,8 @@ void main()
discard; discard;
} }
vec3 norm = normalize(texture2DRect(normalMap, frag.xy).xyz*2.0-1.0); vec3 norm = texture2DRect(normalMap, frag.xy).xyz;
norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
vec4 spec = texture2DRect(specularRect, frag.xy); vec4 spec = texture2DRect(specularRect, frag.xy);
vec3 diff = texture2DRect(diffuseRect, frag.xy).rgb; vec3 diff = texture2DRect(diffuseRect, frag.xy).rgb;
float noise = texture2D(noiseMap, frag.xy/128.0).b; float noise = texture2D(noiseMap, frag.xy/128.0).b;
......
...@@ -55,7 +55,8 @@ void main() ...@@ -55,7 +55,8 @@ void main()
discard; discard;
} }
vec3 norm = texture2DRect(normalMap, frag.xy).xyz*2.0-1.0; vec3 norm = texture2DRect(normalMap, frag.xy).xyz;
norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
float da = dot(norm, lv); float da = dot(norm, lv);
if (da < 0.0) if (da < 0.0)
{ {
......
...@@ -38,10 +38,10 @@ vec4 getPosition(vec2 pos_screen) ...@@ -38,10 +38,10 @@ vec4 getPosition(vec2 pos_screen)
void main() void main()
{ {
vec3 norm = texture2DRect(normalMap, vary_fragcoord.xy).xyz*2.0-1.0; vec3 norm = texture2DRect(normalMap, vary_fragcoord.xy).xyz;
norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
vec3 pos = getPosition(vary_fragcoord.xy).xyz; vec3 pos = getPosition(vary_fragcoord.xy).xyz;
vec3 ccol = texture2DRect(giLightMap, vary_fragcoord.xy).rgb; vec3 ccol = texture2DRect(giLightMap, vary_fragcoord.xy).rgb;
vec2 dlt = kern_scale * delta/(1.0+norm.xy*norm.xy); vec2 dlt = kern_scale * delta/(1.0+norm.xy*norm.xy);
dlt /= max(-pos.z*dist_factor, 1.0); dlt /= max(-pos.z*dist_factor, 1.0);
...@@ -51,7 +51,8 @@ void main() ...@@ -51,7 +51,8 @@ void main()
for (int i = 0; i < kern_length; i++) for (int i = 0; i < kern_length; i++)
{ {
vec2 tc = vary_fragcoord.xy + kern[i].y*dlt; vec2 tc = vary_fragcoord.xy + kern[i].y*dlt;
vec3 sampNorm = texture2DRect(normalMap, tc.xy).xyz*2.0-1.0; vec3 sampNorm = texture2DRect(normalMap, tc.xy).xyz;
sampNorm = vec3((sampNorm.xy-0.5)*2.0,sampNorm.z); // unpack norm
float d = dot(norm.xyz, sampNorm); float d = dot(norm.xyz, sampNorm);
......
...@@ -259,7 +259,8 @@ void main() ...@@ -259,7 +259,8 @@ void main()
vec2 tc = vary_fragcoord.xy; vec2 tc = vary_fragcoord.xy;
float depth = texture2DRect(depthMap, tc.xy).a; float depth = texture2DRect(depthMap, tc.xy).a;
vec3 pos = getPosition_d(tc, depth).xyz; vec3 pos = getPosition_d(tc, depth).xyz;
vec3 norm = texture2DRect(normalMap, tc).xyz*2.0-1.0; vec3 norm = texture2DRect(normalMap, tc).xyz;
norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
//vec3 nz = texture2D(noiseMap, vary_fragcoord.xy/128.0).xyz; //vec3 nz = texture2D(noiseMap, vary_fragcoord.xy/128.0).xyz;
float da = max(dot(norm.xyz, vary_light.xyz), 0.0); float da = max(dot(norm.xyz, vary_light.xyz), 0.0);
...@@ -310,7 +311,8 @@ void main() ...@@ -310,7 +311,8 @@ void main()
texture2DRect(diffuseRect, ref2d + vec2(-checkoffset, 0.0)).rgb); texture2DRect(diffuseRect, ref2d + vec2(-checkoffset, 0.0)).rgb);
float refdepth = texture2DRect(depthMap, ref2d).a; float refdepth = texture2DRect(depthMap, ref2d).a;
vec3 refpos = getPosition_d(ref2d, refdepth).xyz; vec3 refpos = getPosition_d(ref2d, refdepth).xyz;
vec3 refn = normalize(texture2DRect(normalMap, ref2d).rgb * 2.0 - 1.0); vec3 refn = texture2DRect(normalMap, ref2d).rgb;
refn = normalize(vec3((refn.xy-0.5)*2.0,refn.z)); // unpack norm
// figure out how appropriate our guess actually was // figure out how appropriate our guess actually was
float refapprop = max(0.0, dot(-refnorm, normalize(pos - refpos))); float refapprop = max(0.0, dot(-refnorm, normalize(pos - refpos)));
// darken reflections from points which face away from the reflected ray - our guess was a back-face // darken reflections from points which face away from the reflected ray - our guess was a back-face
......
...@@ -68,7 +68,8 @@ void main() ...@@ -68,7 +68,8 @@ void main()
discard; discard;
} }
vec3 norm = texture2DRect(normalMap, frag.xy).xyz*2.0-1.0; vec3 norm = texture2DRect(normalMap, frag.xy).xyz;
norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
norm = normalize(norm); norm = normalize(norm);
float l_dist = -dot(lv, proj_n); float l_dist = -dot(lv, proj_n);
......
...@@ -114,7 +114,8 @@ void main() ...@@ -114,7 +114,8 @@ void main()
vec4 pos = getPosition(pos_screen); vec4 pos = getPosition(pos_screen);
vec3 norm = texture2DRect(normalMap, pos_screen).xyz*2.0-1.0; vec3 norm = texture2DRect(normalMap, pos_screen).xyz;
norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
gl_FragColor[0] = 1.0; gl_FragColor[0] = 1.0;
gl_FragColor[1] = calcAmbientOcclusion(pos, norm); gl_FragColor[1] = calcAmbientOcclusion(pos, norm);
......
...@@ -29,6 +29,7 @@ void main() ...@@ -29,6 +29,7 @@ void main()
gl_FragData[0] = vec4(outColor.rgb, 0.0); gl_FragData[0] = vec4(outColor.rgb, 0.0);
gl_FragData[1] = vec4(outColor.rgb*0.2, 0.2); gl_FragData[1] = vec4(outColor.rgb*0.2, 0.2);
gl_FragData[2] = vec4(normalize(vary_normal)*0.5+0.5, 0.0); vec3 nvn = normalize(vary_normal);
gl_FragData[2] = vec4(nvn.xy * 0.5 + 0.5, nvn.z, 0.0);
} }
...@@ -14,5 +14,6 @@ void main() ...@@ -14,5 +14,6 @@ void main()
vec4 col = texture2D(diffuseMap, gl_TexCoord[0].xy); vec4 col = texture2D(diffuseMap, gl_TexCoord[0].xy);
gl_FragData[0] = vec4(gl_Color.rgb*col.rgb, col.a <= 0.5 ? 0.0 : 0.005); gl_FragData[0] = vec4(gl_Color.rgb*col.rgb, col.a <= 0.5 ? 0.0 : 0.005);
gl_FragData[1] = vec4(0,0,0,0); gl_FragData[1] = vec4(0,0,0,0);
gl_FragData[2] = vec4(normalize(vary_normal)*0.5+0.5, 0.0); vec3 nvn = normalize(vary_normal);
gl_FragData[2] = vec4(nvn.xy * 0.5 + 0.5, nvn.z, 0.0);
} }
...@@ -137,5 +137,5 @@ void main() ...@@ -137,5 +137,5 @@ void main()
gl_FragData[0] = vec4(color.rgb, 0.5); // diffuse gl_FragData[0] = vec4(color.rgb, 0.5); // diffuse
gl_FragData[1] = vec4(0.5,0.5,0.5, 0.95); // speccolor*spec, spec gl_FragData[1] = vec4(0.5,0.5,0.5, 0.95); // speccolor*spec, spec
gl_FragData[2] = vec4(screenspacewavef*0.5+0.5, screenspacewavef.z*0.5); // normal, displace gl_FragData[2] = vec4(screenspacewavef.xy*0.5+0.5, screenspacewavef.z, screenspacewavef.z*0.5); // normalxyz, displace
} }
...@@ -37,7 +37,8 @@ vec4 getPosition(vec2 pos_screen) ...@@ -37,7 +37,8 @@ vec4 getPosition(vec2 pos_screen)
void main() void main()
{ {
vec3 norm = texture2DRect(normalMap, vary_fragcoord.xy).xyz*2.0-1.0; vec3 norm = texture2DRect(normalMap, vary_fragcoord.xy).xyz;
norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
vec3 pos = getPosition(vary_fragcoord.xy).xyz; vec3 pos = getPosition(vary_fragcoord.xy).xyz;
vec4 ccol = texture2DRect(lightMap, vary_fragcoord.xy).rgba; vec4 ccol = texture2DRect(lightMap, vary_fragcoord.xy).rgba;
......
...@@ -31,7 +31,8 @@ float getDepth(vec2 pos_screen) ...@@ -31,7 +31,8 @@ float getDepth(vec2 pos_screen)
void main() void main()
{ {
vec3 norm = texture2DRect(normalMap, vary_fragcoord.xy).xyz*2.0-1.0; vec3 norm = texture2DRect(normalMap, vary_fragcoord.xy).xyz;
norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
float depth = getDepth(vary_fragcoord.xy); float depth = getDepth(vary_fragcoord.xy);
vec2 tc = vary_fragcoord.xy; vec2 tc = vary_fragcoord.xy;
...@@ -46,8 +47,12 @@ void main() ...@@ -46,8 +47,12 @@ void main()
de = step(depth_cutoff, de); de = step(depth_cutoff, de);
vec2 ne; vec2 ne;
ne.x = dot(texture2DRect(normalMap, tc+vec2(-sc,-sc)).rgb*2.0-1.0, norm); vec3 nexnorm = texture2DRect(normalMap, tc+vec2(-sc,-sc)).rgb;
ne.y = dot(texture2DRect(normalMap, tc+vec2(sc,sc)).rgb*2.0-1.0, norm); nexnorm = vec3((nexnorm.xy-0.5)*2.0,nexnorm.z); // unpack norm
ne.x = dot(nexnorm, norm);
vec3 neynorm = texture2DRect(normalMap, tc+vec2(sc,sc)).rgb;
neynorm = vec3((neynorm.xy-0.5)*2.0,neynorm.z); // unpack norm
ne.y = dot(neynorm, norm);
ne = 1.0-ne; ne = 1.0-ne;
......
...@@ -129,7 +129,8 @@ void main() ...@@ -129,7 +129,8 @@ void main()
shadow = min(sh[proj_shadow_idx]+shadow_fade, 1.0); shadow = min(sh[proj_shadow_idx]+shadow_fade, 1.0);
} }
vec3 norm = texture2DRect(normalMap, frag.xy).xyz*2.0-1.0; vec3 norm = texture2DRect(normalMap, frag.xy).xyz;
norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
norm = normalize(norm); norm = normalize(norm);
float l_dist = -dot(lv, proj_n); float l_dist = -dot(lv, proj_n);
......
...@@ -258,7 +258,8 @@ void main() ...@@ -258,7 +258,8 @@ void main()
vec2 tc = vary_fragcoord.xy; vec2 tc = vary_fragcoord.xy;
float depth = texture2DRect(depthMap, tc.xy).a; float depth = texture2DRect(depthMap, tc.xy).a;
vec3 pos = getPosition_d(tc, depth).xyz; vec3 pos = getPosition_d(tc, depth).xyz;
vec3 norm = texture2DRect(normalMap, tc).xyz*2.0-1.0; vec3 norm = texture2DRect(normalMap, tc).xyz;
norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
//vec3 nz = texture2D(noiseMap, vary_fragcoord.xy/128.0).xyz; //vec3 nz = texture2D(noiseMap, vary_fragcoord.xy/128.0).xyz;
float da = max(dot(norm.xyz, vary_light.xyz), 0.0); float da = max(dot(norm.xyz, vary_light.xyz), 0.0);
...@@ -310,7 +311,9 @@ void main() ...@@ -310,7 +311,9 @@ void main()
float refdepth = texture2DRect(depthMap, ref2d).a; float refdepth = texture2DRect(depthMap, ref2d).a;
vec3 refpos = getPosition_d(ref2d, refdepth).xyz; vec3 refpos = getPosition_d(ref2d, refdepth).xyz;
float refshad = texture2DRect(lightMap, ref2d).r; float refshad = texture2DRect(lightMap, ref2d).r;
vec3 refn = normalize(texture2DRect(normalMap, ref2d).rgb * 2.0 - 1.0); vec3 refn = texture2DRect(normalMap, ref2d).rgb;
refn = vec3((refn.xy-0.5)*2.0,refn.z); // unpack norm
refn = normalize(refn);
// figure out how appropriate our guess actually was // figure out how appropriate our guess actually was
float refapprop = max(0.0, dot(-refnorm, normalize(pos - refpos))); float refapprop = max(0.0, dot(-refnorm, normalize(pos - refpos)));
// darken reflections from points which face away from the reflected ray - our guess was a back-face // darken reflections from points which face away from the reflected ray - our guess was a back-face
......
...@@ -82,7 +82,8 @@ void main() ...@@ -82,7 +82,8 @@ void main()
discard; discard;
} }
vec3 norm = texture2DRect(normalMap, frag.xy).xyz*2.0-1.0; vec3 norm = texture2DRect(normalMap, frag.xy).xyz;
norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
norm = normalize(norm); norm = normalize(norm);
float l_dist = -dot(lv, proj_n); float l_dist = -dot(lv, proj_n);
......
...@@ -104,8 +104,9 @@ void main() ...@@ -104,8 +104,9 @@ void main()
vec4 pos = getPosition(pos_screen); vec4 pos = getPosition(pos_screen);
vec4 nmap4 = texture2DRect(normalMap, pos_screen); vec4 nmap4 = texture2DRect(normalMap, pos_screen);
nmap4 = vec4((nmap4.xy-0.5)*2.0,nmap4.z,nmap4.w); // unpack norm
float displace = nmap4.w; float displace = nmap4.w;
vec3 norm = nmap4.xyz*2.0-1.0; vec3 norm = nmap4.xyz;
/*if (pos.z == 0.0) // do nothing for sky *FIX: REMOVE THIS IF/WHEN THE POSITION MAP IS BEING USED AS A STENCIL /*if (pos.z == 0.0) // do nothing for sky *FIX: REMOVE THIS IF/WHEN THE POSITION MAP IS BEING USED AS A STENCIL
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment