Sunday, June 24, 2007

[GPU] Ocean simulation





I have been working on my ocean simulation again - It's been a lot of fun to 'resurect' this code.



The two main improvements are the addition of foam, and the 'sparkles' in areas reflecting the sun. I also added a very bad looking boat (ok, this will require more work ;-)

I was afraid adding foam would be difficult, but actually the simplest approach turned out quite well. The foam is a (toroidal) texture mapped onto the sea.

The texture I use is shown here - notice it is very dark - That's because I simply add its color to the sea color.



The foam layer is multiplied by a spatially varying control value. When the control value is 0 the foam is invisible, when 1 it is fully visible. The idea is to have foam appear where the sea surface becomes rough. To do this, I am simply using (1-sea_nrm.z) as the control value. It does work reasonably well. One improvement could be to 'blur' this control value a little bit, but the current approach seems sufficient.


Last trick: To avoid obvious repetitions, I in fact add two versions of the foam texture with different scaling values. (eg. One is repeating every 2.7 units, the other every 3.14 units).


The second improvement is the addition of 'sparkles' of light. If you've been lucky enough to witness great sunset on the sea, you may have noticed all these small 'random' light sparkles in the distance. These are due to small waves reflecting the sun light directly into your eyes. I was hoping that adding sparkles would give a better impression of detail in the distance. (Especially because we do lose detail in the distance due to filtering of higher-frequency waves).



The sparkles are generated in a post-processing step. First, I render the scene in a render target. I use this texture to compute a 'probability' for sparkle appearance. Basically, sparkles are likely to appear in areas reflecting the sun light.





Second, I render the sparkles in another render target. I use a random number generator and compare its output to the probability of appearance. If the random number is large enough a white pixel is output. Otherwise a black pixel is written. Of course the generated random numbers vary with time.

This image shows the 'sparkle' layer which is directly added to the final result. I was quite happy with the result - even though it is perfectible. I think it is quite successful in increasing the impression of detail on the ocean surface.

To conclude, here is the final result. Hopefully you can see the sparkles even at this resolution.



Next I am planning to add the reflection of the boat in the sea and implement some more credible boat dynamics (not sure about this one - will depend on the gameplay).

Of course I'd like to improve the boat model / rendering. My plan (in a world with no time constraints) would be to create a boat race - or a 'boat-war' game. So if you are a modeler please send me your best creations ;-)

7 comments:

  1. Congratulations. Great job!!

    For me it is looking awesome.

    Any chances to get a source code?

    ReplyDelete
  2. Thanks :-) You can get the full source code
    here

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. It is great. But I can not see the boat ? How can I show it?

    Thank you!

    Jack

    ReplyDelete
  5. This is awesome!
    May I get the source code,too?
    plz
    953003044@cc.ncu.edu.tw
    thanks
    (I tried the download link,but it's dead.)

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete