Man Versus Code

The personal website of Joe Marrero.

Rain Simulation

This is a program that simulates a rain storm. The simulation displays rain falling down on to a watery surface. As the rain falls, numerous ripples are created on the water's surface. This simulation uses OpenGL.

The Wave Equation

This technique was not invented by me. I first read about this here.

The following wave equations were used to create the ripple effects on the water surface. This surface is a 2D mesh of vertices whose heights are adjusted with the wave equation below. The equation requires two conditions to be satisfied, otherwise the heights will shoot off to infinity.

Wave equation formulas

In the above equations i and j are the two dimensional position along the surface, and k represents a moment in time. So k would mean the moment in time right now and k + 1 would be the next moment in the future. Also z represents the height, c is the wave speed, µ is the viscosity of the water, and t is time (really a time step).

Implementation

The way one can implement the above wave equation in code is to have two heightmaps. A simple heightmap can be implemented as a two dimensional array of floats. As for c, µ and t the values can be passed at the beginning. In fact, it should be noted that t is a timestep and has nothing to do with getting the time from a timer. t should be calculated in relation to the time in takes to draw one frame. This ensures the animation speeds up and down when necessary. So t has to do with how fast the overall changes in the wave equation appear. Furthermore, all three coefficients can be precomputed and used whenever you are updating the "future" heightmap.

The way to draw the mesh on one complete pass is to draw a degenerate triangle linking the end point with the start of the next row. Then to draw the complete mesh in one pass using a vertex buffer. The implementation in the source code does not use this for clearity and simplicity.

Minimum Requirements

  • 64 MB VGA card with OpenGL hardware acceleration.
  • Pentium 4 or AMD Athlon 64, 3.0GHz
  • 1 GB RAM


Screenshots

Screenshot 26 Image