How it works...

The vertex shader takes the position of the vertex and updates the y coordinate using the wave equation discussed previously. After the first three statements, the variable pos is just a copy of the VertexPosition input variable with the modified y coordinate.

We then compute the normal vector using the previous equation, normalize the result, and store it in the n variable. Since the wave is really just a two-dimensional wave (it doesn't depend on z), the z component of the normal vector will be zero.

Finally, we pass along the new position and normal to the fragment shader after converting to camera coordinates. As usual, we also pass the position in clip coordinates to the built-in gl_Position variable.

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.