Use Blinn Phong model.

This commit is contained in:
vlj 2014-04-06 21:23:09 +02:00 committed by Vincent Lejeune
parent 0b318b9c7e
commit cafc07680a
3 changed files with 41 additions and 37 deletions

View File

@ -14,36 +14,34 @@ out vec4 Specular;
vec3 DecodeNormal(vec2 n); vec3 DecodeNormal(vec2 n);
void main() { void main()
vec2 texc = gl_FragCoord.xy / screen; {
float z = texture(dtex, texc).x; vec2 texc = gl_FragCoord.xy / screen;
vec3 norm = normalize(DecodeNormal(2. * texture(ntex, texc).xy - 1.)); float z = texture(dtex, texc).x;
vec3 norm = normalize(DecodeNormal(2. * texture(ntex, texc).xy - 1.));
//float roughness = texture(ntex, texc).z;
vec4 xpos = 2.0 * vec4(texc, z, 1.0) - 1.0f; vec4 xpos = 2.0 * vec4(texc, z, 1.0) - 1.0f;
xpos = invproj * xpos; xpos = invproj * xpos;
xpos /= xpos.w; xpos /= xpos.w;
vec3 eyedir = normalize(xpos.xyz); vec3 eyedir = -normalize(xpos.xyz);
vec3 diffuse = vec3(0.), specular = vec3(0.); vec4 pseudocenter = ViewMatrix * vec4(center.xyz, 1.0);
pseudocenter /= pseudocenter.w;
vec3 light_pos = pseudocenter.xyz;
vec3 light_col = col.xyz;
float d = distance(light_pos, xpos.xyz);
float att = energy * 200. / (4. * 3.14 * d * d);
float spec_att = (energy + 10.) * 200. / (4. * 3.14 * d * d);
vec4 pseudocenter = ViewMatrix * vec4(center.xyz, 1.0); // Light Direction
pseudocenter /= pseudocenter.w; vec3 L = -normalize(xpos.xyz - light_pos);
vec3 light_pos = pseudocenter.xyz; // Half Light View direction
vec3 light_col = col.xyz; vec3 H = normalize(eyedir + L);
float d = distance(light_pos, xpos.xyz);
float att = energy * 200. / (4. * 3.14 * d * d);
float spec_att = (energy + 10.) * 200. / (4. * 3.14 * d * d);
// Light Direction float NdotL = max(0., dot(norm, L));
vec3 L = normalize(xpos.xyz - light_pos); float NdotH = max(0., dot(norm, H));
float NdotL = max(0.0, dot(norm, -L)); Diffuse = vec4(NdotL * light_col * att, 1.);
diffuse += NdotL * light_col * att; Specular = vec4(pow(NdotH, spec) * light_col * NdotL * spec_att, 1.);
// Reflected light dir
vec3 R = reflect(-L, norm);
float RdotE = max(0.0, dot(R, eyedir));
specular += pow(RdotE, spec) * light_col * spec_att;
Diffuse = vec4(diffuse, 1.);
Specular = vec4(specular , 1.);
} }

View File

@ -36,14 +36,17 @@ void main() {
} }
vec3 norm = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.)); vec3 norm = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.));
vec3 eyedir = -normalize(xpos.xyz);
// Normalized on the cpu // Normalized on the cpu
vec3 L = direction; vec3 L = direction;
// Half Light View direction
vec3 H = normalize(eyedir + L);
float NdotL = max(0.0, dot(norm, L)); float NdotL = max(0., dot(norm, L));
vec3 R = reflect(L, norm); float NdotH = max(0., dot(norm, H));
float RdotE = max(0.0, dot(R, normalize(xpos.xyz)));
float Specular = pow(RdotE, 200); float Specular = pow(NdotH, 200) * NdotL;
vec3 outcol = NdotL * col; vec3 outcol = NdotL * col;

View File

@ -64,14 +64,17 @@ void main() {
xpos.xyz /= xpos.w; xpos.xyz /= xpos.w;
vec3 norm = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.)); vec3 norm = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.));
vec3 eyedir = -normalize(xpos.xyz);
// Normalized on the cpu // Normalized on the cpu
vec3 L = direction; vec3 L = direction;
// Half Light View direction
vec3 H = normalize(eyedir + L);
float NdotL = max(0.0, dot(norm, L)); float NdotL = max(0., dot(norm, L));
vec3 R = reflect(L, norm); float NdotH = max(0., dot(norm, H));
float RdotE = max(0.0, dot(R, normalize(xpos.xyz)));
float Specular = pow(RdotE, 200); float Specular = pow(NdotH, 200) * NdotL;
vec3 outcol = NdotL * col; vec3 outcol = NdotL * col;