Skip to content

Commit

Permalink
Merge pull request glium#1940 from marcianx/master
Browse files Browse the repository at this point in the history
Correct formulas in the normal mapping tutorial.
  • Loading branch information
est31 committed May 10, 2022
2 parents 1b746cd + a45f428 commit b9bffaf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion book/tuto-14-wall.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ Thanks to this we can calculate the *real* normal, in other words the normal of
at the given pixel:

```glsl
mat3 tbn = cotangent_frame(v_normal, v_position, v_tex_coords);
mat3 tbn = cotangent_frame(v_normal, -v_position, v_tex_coords);
vec3 real_normal = normalize(tbn * -(normal_map * 2.0 - 1.0));
```

Expand Down
11 changes: 8 additions & 3 deletions examples/tutorial-14.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ fn main() {
vec3 diffuse_color = texture(diffuse_tex, v_tex_coords).rgb;
vec3 ambient_color = diffuse_color * 0.1;
vec3 v_normal_unit = normalize(v_normal);
vec3 normal_map = texture(normal_tex, v_tex_coords).rgb;
mat3 tbn = cotangent_frame(v_normal, v_position, v_tex_coords);
mat3 tbn = cotangent_frame(v_normal_unit, -v_position, v_tex_coords);
vec3 real_normal = normalize(tbn * -(normal_map * 2.0 - 1.0));
float diffuse = max(dot(real_normal, normalize(u_light)), 0.0);
Expand All @@ -117,6 +118,7 @@ fn main() {
let program = glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src,
None).unwrap();

let start = std::time::Instant::now();
event_loop.run(move |event, _, control_flow| {
let next_frame_time = std::time::Instant::now() +
std::time::Duration::from_nanos(16_666_667);
Expand All @@ -141,10 +143,13 @@ fn main() {
let mut target = display.draw();
target.clear_color_and_depth((0.0, 0.0, 1.0, 1.0), 1.0);

let t = (std::time::Instant::now() - start).as_secs_f32() * 2.0;
let ang = t.sin();
let (c, s) = (ang.cos(), ang.sin());
let model = [
[1.0, 0.0, 0.0, 0.0],
[ c, 0.0, s, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[ -s, 0.0, c, 0.0],
[0.0, 0.0, 0.0, 1.0f32]
];

Expand Down

0 comments on commit b9bffaf

Please sign in to comment.