How to do it...

In the vertex shader, we translate the y coordinate of the vertex:

layout (location = 0) in vec3 VertexPosition; out vec4 Position; out vec3 Normal; uniform float Time; // The animation time // Wave parameters uniform float K; // Wavenumber uniform float Velocity; // Wave's velocity uniform float Amp; // Wave's amplitude uniform mat4 ModelViewMatrix; uniform mat3 NormalMatrix; uniform mat4 MVP; void main() { vec4 pos = vec4(VertexPosition,1.0); // Translate the y coordinate float u = K * (pos.x - Velocity * Time); pos.y = Amp * sin( u ); // Compute the normal vector vec3 n = vec3(0.0); n.xy = normalize(vec2(-K * Amp *cos( u ), 1.0)); // Send position and normal (in camera cords) to frag. Position = ModelViewMatrix * pos; Normal ...

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.