Fix sun area light

This commit is contained in:
Vincent Lejeune 2014-11-21 02:44:27 +01:00
parent 79e5e9f880
commit f3a637fb99
7 changed files with 33 additions and 23 deletions

View File

@ -49,7 +49,7 @@ void main(void)
vec3 sampleDirection = reflect(-eyedir, normal); vec3 sampleDirection = reflect(-eyedir, normal);
sampleDirection = (InverseViewMatrix * vec4(sampleDirection, 0.)).xyz; sampleDirection = (InverseViewMatrix * vec4(sampleDirection, 0.)).xyz;
float specval = pow(texture(ntex, uv).z, 2.); float specval = texture(ntex, uv).z;
// From http://graphics.cs.williams.edu/papers/EnvMipReport2013/ // From http://graphics.cs.williams.edu/papers/EnvMipReport2013/
int texSize = textureSize(tex, 0).x; int texSize = textureSize(tex, 0).x;
float lodval = clamp(log2(texSize * sqrt(3.)) - .5 * log2(specval + 1.), 0., 10.); float lodval = clamp(log2(texSize * sqrt(3.)) - .5 * log2(specval + 1.), 0., 10.);

View File

@ -19,5 +19,5 @@ void main(void)
float glossmap = texture(glosstex, uv).x; float glossmap = texture(glosstex, uv).x;
#endif #endif
EncodedNormal.xy = 0.5 * EncodeNormal(normalize(nor)) + 0.5; EncodedNormal.xy = 0.5 * EncodeNormal(normalize(nor)) + 0.5;
EncodedNormal.z = exp2(10. * glossmap + 1.); EncodedNormal.z = exp2(20. * glossmap + 1.);
} }

View File

@ -24,6 +24,6 @@ void main() {
if (col.a < 0.5) if (col.a < 0.5)
discard; discard;
EncodedNormal.xy = 0.5 * EncodeNormal(normalize(nor)) + 0.5; EncodedNormal.xy = 0.5 * EncodeNormal(normalize(nor)) + 0.5;
EncodedNormal.z = exp2(10. * glossmap + 1.); EncodedNormal.z = exp2(20. * glossmap + 1.);
} }

View File

@ -19,5 +19,5 @@ void main(void)
{ {
float glossmap = texture(tex, uv).x; float glossmap = texture(tex, uv).x;
EncodedNormal.xy = 0.5 * EncodeNormal(normalize(nor)) + 0.5; EncodedNormal.xy = 0.5 * EncodeNormal(normalize(nor)) + 0.5;
EncodedNormal.z = exp2(10. * glossmap + 1.); EncodedNormal.z = exp2(20. * glossmap + 1.);
} }

View File

@ -24,6 +24,6 @@ void main() {
discard; discard;
float glossmap = texture(glosstex, uv).x; float glossmap = texture(glosstex, uv).x;
EncodedNormal.xy = 0.5 * EncodeNormal(normalize(nor)) + 0.5; EncodedNormal.xy = 0.5 * EncodeNormal(normalize(nor)) + 0.5;
EncodedNormal.z = exp2(10. * glossmap + 1.); EncodedNormal.z = exp2(20. * glossmap + 1.);
} }

View File

@ -16,6 +16,16 @@ vec3 DecodeNormal(vec2 n);
vec3 getSpecular(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness); vec3 getSpecular(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness);
vec4 getPosFromUVDepth(vec3 uvDepth, mat4 InverseProjectionMatrix); vec4 getPosFromUVDepth(vec3 uvDepth, mat4 InverseProjectionMatrix);
vec3 getMostRepresentativePoint(vec3 direction, vec3 R, float angularRadius)
{
vec3 D = direction;
float d = cos(angularRadius);
float r = sin(angularRadius);
float DdotR = dot(D, R);
vec3 S = R - DdotR * D;
return (DdotR < d) ? normalize(d * D + normalize (S) * r) : R;
}
void main() { void main() {
vec2 uv = gl_FragCoord.xy / screen; vec2 uv = gl_FragCoord.xy / screen;
float z = texture(dtex, uv).x; float z = texture(dtex, uv).x;
@ -38,16 +48,11 @@ void main() {
float NdotL = max(0., dot(norm, L)); float NdotL = max(0., dot(norm, L));
vec3 D = direction; float angle = 3.14 * sunangle / 180.;
vec3 R = reflect(eyedir, norm); vec3 R = reflect(-eyedir, norm);
float angle = 3.14 * sunangle / 180; vec3 Lightdir = getMostRepresentativePoint(direction, R, angle);
float d = cos(angle);
float r = sin(angle);
float DdotR = dot (D, R);
vec3 S = R - DdotR * D;
vec3 Lightdir = DdotR < d ? normalize (d * D + normalize (S) * r) : R;
vec3 Specular = getSpecular(norm, eyedir, L, col, roughness) * dot(Lightdir, norm); vec3 Specular = getSpecular(norm, eyedir, Lightdir, col, roughness) * NdotL;
vec3 outcol = NdotL * col; vec3 outcol = NdotL * col;

View File

@ -19,6 +19,16 @@ vec3 DecodeNormal(vec2 n);
vec3 getSpecular(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness); vec3 getSpecular(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness);
vec4 getPosFromUVDepth(vec3 uvDepth, mat4 InverseProjectionMatrix); vec4 getPosFromUVDepth(vec3 uvDepth, mat4 InverseProjectionMatrix);
vec3 getMostRepresentativePoint(vec3 direction, vec3 R, float angularRadius)
{
vec3 D = direction;
float d = cos(angularRadius);
float r = sin(angularRadius);
float DdotR = dot(D, R);
vec3 S = R - DdotR * D;
return (DdotR < d) ? normalize(d * D + normalize (S) * r) : R;
}
float getShadowFactor(vec3 pos, float bias, int index) float getShadowFactor(vec3 pos, float bias, int index)
{ {
@ -52,16 +62,11 @@ void main() {
float NdotL = max(0., dot(norm, L)); float NdotL = max(0., dot(norm, L));
vec3 D = direction; float angle = 3.14 * sunangle / 180.;
vec3 R = reflect(eyedir, norm); vec3 R = reflect(-eyedir, norm);
float angle = 3.14 * sunangle / 180; vec3 Lightdir = getMostRepresentativePoint(direction, R, angle);
float d = cos(angle);
float r = sin(angle);
float DdotR = dot (D, R);
vec3 S = R - DdotR * D;
vec3 Lightdir = DdotR < d ? normalize (d * D + normalize (S) * r) : R;
vec3 Specular = getSpecular(norm, eyedir, Lightdir, col, roughness) * dot(norm, Lightdir); vec3 Specular = getSpecular(norm, eyedir, Lightdir, col, roughness) * NdotL;
vec3 outcol = NdotL * col; vec3 outcol = NdotL * col;