How to do it...

To render an image using normal mapping, use the following shaders:

  1. In the vertex shader, find the object local coordinate system (tangent space) and transform everything into that space. Pass the tangent space light direction and view direction to the fragment shader:
layout (location = 0) in vec3 VertexPosition; layout (location = 1) in vec3 VertexNormal; layout (location = 2) in vec2 VertexTexCoord; layout (location = 3) in vec4 VertexTangent; out vec3 LightDir; out vec2 TexCoord; out vec3 ViewDir; // Other uniform variables... void main() { // Transform normal and tangent to eye space vec3 norm = normalize(NormalMatrix * VertexNormal); vec3 tang = normalize(NormalMatrix * VertexTangent.xyz); // Compute the binormal vec3 ...

Get OpenGL 4 Shading Language Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.