Skip to content

Commit

Permalink
bugfix: re-normalised normals in frag shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
capnramses committed Dec 18, 2017
1 parent b8780c0 commit be691de
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
9 changes: 6 additions & 3 deletions 08_phong/test_fs.glsl
Expand Up @@ -22,26 +22,29 @@ void main () {
// ambient intensity
vec3 Ia = La * Ka;

// normalize in case interpolation has upset normals' lengths
vec3 n_eye = normalize( normal_eye );

// diffuse intensity
// raise light position to eye space
vec3 light_position_eye = vec3 (view_mat * vec4 (light_position_world, 1.0));
vec3 distance_to_light_eye = light_position_eye - position_eye;
vec3 direction_to_light_eye = normalize (distance_to_light_eye);
float dot_prod = dot (direction_to_light_eye, normal_eye);
float dot_prod = dot (direction_to_light_eye, n_eye);
dot_prod = max (dot_prod, 0.0);
vec3 Id = Ld * Kd * dot_prod; // final diffuse intensity

// specular intensity
vec3 surface_to_viewer_eye = normalize (-position_eye);

//vec3 reflection_eye = reflect (-direction_to_light_eye, normal_eye);
//vec3 reflection_eye = reflect (-direction_to_light_eye, n_eye);
//float dot_prod_specular = dot (reflection_eye, surface_to_viewer_eye);
//dot_prod_specular = max (dot_prod_specular, 0.0);
//float specular_factor = pow (dot_prod_specular, specular_exponent);

// blinn
vec3 half_way_eye = normalize (surface_to_viewer_eye + direction_to_light_eye);
float dot_prod_specular = max (dot (half_way_eye, normal_eye), 0.0);
float dot_prod_specular = max (dot (half_way_eye, n_eye), 0.0);
float specular_factor = pow (dot_prod_specular, specular_exponent);

vec3 Is = Ls * Ks * specular_factor; // final specular intensity
Expand Down
7 changes: 5 additions & 2 deletions 15_phongtextures/test_fs.glsl
Expand Up @@ -34,19 +34,22 @@ float specular_exponent = 100.0; // specular 'power'
void main() {
vec3 light_pos_eye = (view * vec4 (light_position_world, 1.0)).xyz;

// normalize in case interpolation has upset normals' lengths
vec3 n_eye = normalize( norm_eye );

vec3 Ka = texture (ambient_map, st).rgb;
vec3 Ia = vec3 (0.2, 0.2, 0.2) * Ka;

vec4 texel = texture (diffuse_map, st);
vec3 Kd = texel.rgb;
vec3 surface_to_light_eye = normalize (light_pos_eye - pos_eye);
float dp = max (0.0, dot (norm_eye, surface_to_light_eye));
float dp = max (0.0, dot (n_eye, surface_to_light_eye));
vec3 Id = Kd * Ld * dp;

vec3 Ks = texture (specular_map, st).rgb;
vec3 surface_to_viewer_eye = normalize (-pos_eye);
vec3 half_way_eye = normalize (surface_to_viewer_eye + surface_to_light_eye);
float dot_prod_specular = max (dot (half_way_eye, norm_eye), 0.0);
float dot_prod_specular = max (dot (half_way_eye, n_eye), 0.0);
float specular_factor = pow (dot_prod_specular, specular_exponent);
vec3 Is = Ls * Ks * specular_factor; // final specular intensity

Expand Down

0 comments on commit be691de

Please sign in to comment.