How to do it...

We can use the same vertex shader as was used in the previous recipe, Using normal maps.  The vertex shader transforms the view direction and the light direction and passes them to the fragment shader. It also passes along the texture coordinate.

The fragment shader uses the tangent space view direction and the height map value at the current texture coordinate to offset the texture coordinates. It then uses the new texture coordinate value to do shading as usual:

in vec3 LightDir;  // Tangent spacein vec2 TexCoord;in vec3 ViewDir;  // Tangent spacelayout(binding=0) uniform sampler2D ColorTex;layout(binding=1) uniform sampler2D NormalMapTex;layout(binding=2) uniform sampler2D HeightMapTex;// Light and material uniforms layout( ...

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.