FFT ocean surface

A displaced ocean surface driven by a real inverse FFT. A Phillips spectrum evolves in frequency space, two compute passes run a shared-memory radix-2 IFFT into a displacement field, and a procedural grid rides it with per-pixel normals, foam and a Fresnel sky reflection under a tunable sunset. Orbit with the mouse; tweak the sea from the panel.

Open fullscreen
// Minimal complex-number helpers (complex value packed as vec2f = re + i*im).
// Pure module: only exported functions.
 
export fn cmul(a: vec2f, b: vec2f) -> vec2f {
  return vec2f(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}
 
export fn cconj(a: vec2f) -> vec2f {
  return vec2f(a.x, -a.y);
}
 
// e^{i*theta} = (cos theta, sin theta)
export fn cexp(theta: f32) -> vec2f {
  return vec2f(cos(theta), sin(theta));
}