Ocean waves, and Shaders
Several years ago, I became interested in simulating an ocean surface with waves. At the time, I wrote a blog post about an algorithm from a paper called "Simulating Ocean Water" by Jerry Tessendorf. I spent some time digging into the maths and implementing it in my Rust Raytracer.
Recently, on a whim, I decided to revisit the algorithm, but this time in a GLSL shader. There are plenty of implementations of Tessendorf already on the ShaderToy site, but they were all pretty convoluted, and I wanted an opportunity to dig in myself and really understand the process.
In particular, implementing an Inverse Fourier Transform in shaders was quite a challenge. I settled on a version of the IFT that uses 2 phases to complete the transform. ShaderToy is quite limited - it would be far better to be able to compute this in a compute shader rather than a fragment shader, but given the constraints, it was interesting to give it a go.
I've always considered shaders to be an elegant DSL hampered by a primitive developer experience, and this was no exception. Debugging with red pixels is quite a step back from real software development.
I've been experimenting with AI assisted coding recently, and gave it a shot here. It was an interesting experience - the LLM was very useful up to a point, and yet utterly unable to debug beyond a certain complexity, and I think I was slowed overall by the tool, despite the initial speed. Another anecdote to add to my repertoire of LLM experimentation.
I finally cracked the issue - I was normalizing the sign of the FFT inside the loop instead of outside. In the process though, I made a nice debugging shader:
This shows the various steps to create the wave heightfield, starting with the spectrum. Shifting the phase over time, then running the IFFT.