Sign in to follow this  
yosef

Water

Recommended Posts

Hellow everybody. I try to program water wave effect. I have read articles of Tom Hammersley and Roy Willemse which explain this effect and algorithms details. Firstly I decided to implement "Water Update" and to draw result "waves" as height map greyscale bitmap. Here is fragment of my code: const width = 256, height = 256; BYTE screen[width * height * 3]; // 24 bits bitmap rendered float buffer[width * height][2]; // water heights int cur,prev; . . . void Init() { for(int i=0; i<width*heigh; i++) // set buffer to 0 { buffer[i][0] = buffer[i][1] = 0; } buffer[100][0] = 100; // first value for waves cur = 0; prev = 1; } void Animate(HWND hWnd) { int pos; float h,min = 1000, max = -1000,range; // update water for(int y=1; y<height - 1; y++) for(int x=0; x<width - 1; x++) { pos = Address(x,y); h = ((buffer[Address(x+1,y)][prev] + buffer[Address(x-1,y)]prev]+ buffer[Address(x,y+1)][prev] + buffer[Address(x,y-1)][prev]) / 2) - buffer[pos][cur]; h -= h / 100; buffer[pos][cur] = h; min = (h < min ? h : min); max = (h > max ? h : max); } // normalize heights to 0..255 range = max - min; float scale = 255 / range; for(int i=0,j=0; i<size3; i+=3,j++) { screen[i]=screen[i+1] = screen[i+2] = (buffer[j][cur] - min) * scale; } // swap buffers cur = (cur + 1) & 1; prev = (prev + 1) & 1; } In difference of code in the articles I had to declare buffer as float (not integer) and to do height normalization to 0..255. Besides it the finaly image (waves) was not very similiar to water. Tell me please what is the problem in my code and how to solve it ? Thank you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this