Sign in to follow this  
ProjectinMatrix

N64 Quality Water...

Recommended Posts

Hi again all.

I'm trying to mimic some old Nintendo 64 quality water...
The key points would be to interact with the water and to be able to run it on a low end android device. I'd like the water to animate, and for objects to be able to interact; but it doesn't need to be super realistic (IE: no reflection)

The issue is i'm having a hard time finding resources on renderin water that aren't about particle systems needing the latest and greatest hardware :(

Can anyone please point me in the right direction?

Share this post


Link to post
Share on other sites
The surface looks like it's just a triangle mesh, rendered after the scene with a surface texture and the waves are done by shifting the verticies up and down, probably in the shader. It's probably just a simple set of sine waves of different frequencies added together. It's fast and also easily reproducible for when you want to do (say) collision detection.

The interactions with the objects can be done in two ways.

Firstly, for decorative objects (say things floating on the surface of the water) they can be drawn with a shader which just computes the same sort of offset as the waves based on their position and adds that to all coordinates.

For player objects you'd probably do the position calculation on the CPU and send them prepositioned. This allows them to do things like sink into the water upon landing.

Scatter water splash particle effects around at all the interaction points based on what interaction is happening and that'll help obscure some of the polygon edge artefacts (although many are still visible in that video).

The only complicated part is going to be doing the bouncey "floating" behaviour for your player objects but it'll end up being a fairly small lump of code run a few times a frame (so efficiency won't be too much of a stress) with a few constant factors which will need tuning by hand until you get a behaviour you're happy with -- it's that that tuning process which will give your game its character and feel.

Share this post


Link to post
Share on other sites
[font=arial, verdana, tahoma, sans-serif][size=2]Thanks Katie.

That's pretty much what i had in mind too, but after some searching i found all sorts of particle this and spring simulation that which made me second guess myself. I also found a cool video on [color="#000000"][font="arial, sans-serif"][url="http://www.youtube.com/watch?v=2VNNva1Nu-4"]Gerstner Waves[/url] which i might try to do. Having characters interact with water is going to be hard, but i think i'll be able to figure something out. Thanks again![/font][/color]
[color="#000000"][font="arial, sans-serif"]
[/font][/color][/size][/font]
[color="#000000"][font="arial, sans-serif"]
[/font][/color]
[color="#000000"][font="arial, sans-serif"][size="4"][b]EDIT:[/b][/size][/font][/color]
[color="#000000"][font="arial, sans-serif"]Actually, i have one more question. I can make a pretty nice wave effect using sinf(x) / x, however i have no idea how to animate this... Or how to combine multiple waves so my water isn't just going left to right... Any suggestions?[/font][/color]

Share this post


Link to post
Share on other sites
[quote name='projectionmatrix' timestamp='1318516236' post='4872204']
[color="#000000"][font="arial, sans-serif"]Actually, i have one more question. I can make a pretty nice wave effect using sinf(x) / x, however i have no idea how to animate this... Or how to combine multiple waves so my water isn't just going left to right... Any suggestions?[/font][/color]
[/quote]

Just add a time coefficient in there - replace x with (x + ct) where t is time and c is some constant you can tweak to adjust the speed. To get waves moving in different directions, instead of x, use some combination of x [i]and[/i] y. To combine waves in different directions, just add them together.

Share this post


Link to post
Share on other sites
One last question:

So, right now in my update function i'm calculating sine twice for every vertex. I kind of want to change this to use a lookup table as my project will be run on an embedded device (Nindento dsi).
I think constructing a lookup table then % the argument of sin to keep it in the lookup range would do the trick, but i don't know how the floating point value of timeCoefficient would effect this.
Can anyone point me in the right direction to using a lookup table for this?


[code]
void Update() {
DWORD thisTime = GetTickCount();
float deltaTime = (float)(thisTime - lastTime) * 0.001f;
lastTime = thisTime;

timeCoefficient += deltaTime;

for (int z = 0; z < WATER_WIDTH; ++z) {
for (int x = 0; x < WATER_HEIGHT; ++x) {
deformData[x][z] = sinf(x + timeCoefficient) / x;
deformData[x][z] += sinf(z + timeCoefficient)/ z;
water[x][z][1] = deformData[x][z];
}
}
}[/code]

Share this post


Link to post
Share on other sites
[quote name='Katie' timestamp='1318500499' post='4872155']
The surface looks like it's just a triangle mesh, rendered after the scene with a surface texture and the waves are done by shifting the verticies up and down, probably in the shader. It's probably just a simple set of sine waves of different frequencies added together. It's fast and also easily reproducible for when you want to do (say) collision detection.
[/quote]

In the shader? that was on N64! :)

Would be interested to see some screenshots of your results PrjM

Share this post


Link to post
Share on other sites
Here's a description of what we used to do back in the old days.
[url="http://freespace.virgin.net/hugo.elias/graphics/x_water.htm"]http://freespace.virgin.net/hugo.elias/graphics/x_water.htm[/url]
When you've got it working you can just write some values into the current buffer for each position where you want objects to interact with the water.
This method is still used for games today when you need to get the water to ripple around objects.

Share this post


Link to post
Share on other sites
[quote name='projectionmatrix' timestamp='1318605018' post='4872528']
Can anyone point me in the right direction to using a lookup table for this?

[/quote]

Take a look at [url="http://www.analog.com/library/analogDialogue/archives/38-08/dds.html"]Direct Digital Synthesis[/url].



No need to even calculate sine values, just keep them in a lookup table.

If a function call at a fixed frequency is available to you, this is an efficient way to calculate sine values:

[code]
#define INC_FREQ 10000 // hertz
#define ACC_BITS 32
#define ACC_VALUES 4294967296
#define SAMPLE_BITS 8 // There are 2^(SAMPLE_BITS) samples

#define FREQ_INCR(f) ((long)f * (long)ACC_VALUES) / INC_FREQ

void UpdateSineWaves()
{
for( int i=0; i<numSineWaves; i++ )
{
sineWaves[i].accumulator += sineWaves[i].frequencyIncrement;
sineWaves[i].amplitude = sineWave[sineWaves[i].accumulator>>(ACC_BITS-SAMPLE_BITS)];
}
}
[/code]

To adjust the frequency of a sine wave, just change the frequency increment. This doesn't affect the phase, so the result is very smooth, like what might be wanted for water waves. ;)

Share this post


Link to post
Share on other sites
@mikiex
Screenshots don't demonstrate the water well, here are the files i have so far [url="http://pastebin.com/9VLiNmsM"]WinMain.cpp[/url] [url="http://pastebin.com/jus0q8vK"]GLWater.h[/url] [url="http://pastebin.com/p8mvg3n3"]GLWater.cpp[/url]

[color="#1C2837"][size="2"]@whitechaos35[/size][/color]
[size="2"][color="#1c2837"]I'm still looking into the method & code snippet you posted, it looks very promising but i haven't been able to implement it (I was trying all weekend)[/color][/size]

[size="2"][color="#1c2837"]@MortenB[/color][/size]
[size="2"][color="#1c2837"]I'm going to be looking into the link you posted; i will need ripples around objects eventually.[/color][/size]

[size="2"][color="#1c2837"]THANKS EVERYONE![/color][/size]

Share this post


Link to post
Share on other sites
[quote name='MortenB' timestamp='1318679138' post='4872800']
Here's a description of what we used to do back in the old days.
[url="http://freespace.virgin.net/hugo.elias/graphics/x_water.htm"]http://freespace.vir...ics/x_water.htm[/url]
When you've got it working you can just write some values into the current buffer for each position where you want objects to interact with the water.
This method is still used for games today when you need to get the water to ripple around objects.
[/quote]

o daaaaaaaamn, i havent been to that site in hella long ;-) I used to live on that site and other similar sites back in 2003-2005'ish. Talk about a blast from the past ;-] I learned how to draw lines, polygons, and do perlin noise off of that site. And their star field tutorial is still the best on the internet.

Share this post


Link to post
Share on other sites
I found a solution (In some old nVidia paper), so far this is acceptable for what i'm doing.
Also: [url="http://pastebin.com/rtyNu4NB"]Updated GLWater.cpp[/url]

[code]
#define MAX_CIRCLE_ANGLE 512
#define HALF_MAX_CIRCLE_ANGLE 256
#define RECIP_HALF_MAX_CIRCLE_ANGLE 0.00390625f
#define QUARTER_MAX_CIRCLE_ANGLE 128
#define MASK_MAX_CIRCLE_ANGLE 511
#define PI 3.1415926535897932f
#define PI_RECIPRICAL 0.3183098861837907f

float fast_cossin_table[MAX_CIRCLE_ANGLE];

float FAST_SIN(float n) {
float f = n * HALF_MAX_CIRCLE_ANGLE * PI_RECIPRICAL;
int i = int(f);
if (i < 0)
return fast_cossin_table[(-((-i)&MASK_MAX_CIRCLE_ANGLE)) + MAX_CIRCLE_ANGLE];
return fast_cossin_table[i&MASK_MAX_CIRCLE_ANGLE];
}

float FAST_COS(float n) {
float f = n * HALF_MAX_CIRCLE_ANGLE * PI_RECIPRICAL;
int i = int(f);
if (i < 0)
return fast_cossin_table[((-i) + QUARTER_MAX_CIRCLE_ANGLE)&MASK_MAX_CIRCLE_ANGLE];
return fast_cossin_table[(i + QUARTER_MAX_CIRCLE_ANGLE)&MASK_MAX_CIRCLE_ANGLE];
}

inline void SetupSinCosLookupTable() {
for (int i = 0 ; i < MAX_CIRCLE_ANGLE ; i++) {
fast_cossin_table[i] = (float)sin((double)i * PI * RECIP_HALF_MAX_CIRCLE_ANGLE);
}
}[/code]

Share this post


Link to post
Share on other sites
So, i've moved on to implementing ripples. So far This bit code causes a circular ripple in the water as one would expect.

[code]void UpdateRippleWater() {
for (int z = 0; z < MAP_WIDTH; ++z) {
for (int x = 0; x < MAP_HEIGHT; ++x) {
float dist = distance(ripple_x, ripple_y, x, z);

deformData[x][z] = FAST_SIN(dist - timeCoefficient * 10.0f) * WAVE_Y_SCALE;

water[x][z][1] = deformData[x][z];
}
}
}[/code]

My question is i want to make the ripple temporary, like someone had dropped an object into the water.
That is have it only start in the center, move outward then calm back down.

Any ideas on how this effect could be achieved?
Thanks for all the help so far everyone!

Share this post


Link to post
Share on other sites
[quote name='projectionmatrix' timestamp='1318876917' post='4873576']
My question is i want to make the ripple temporary, like someone had dropped an object into the water.
That is have it only start in the center, move outward then calm back down.

Any ideas on how this effect could be achieved?
Thanks for all the help so far everyone!
[/quote]


Every point within some radius of the impact point is going to oscillate. Points at an equal distance from the impact are going to be at the same value, which is determined by the phase at that distance and the attenuation value for that distance. A nice looking attenuation wave would look Gaussian.

If you can get the DDS algorithm I provided to work for you, then you can get phase-continuous samples for a sine wave (meaning if you want the frequency to decrease over time it will look perfect). By taking each of these samples and multiplying it by the corresponding attenuation sample, you will construct a "ripple wave". You can then map the distance from impact into a phase-lookup in the ripple table.

This solution eliminates your sine computation *for every value*, so long as you can output a sine wave from the DDS algorithm, which itself doesn't even compute sine values.

You also mention that you were having trouble implementing the DDS algorithm. Are you calling the "UpdateSineWaves" equivalent function at an exact frequency? If so, what is that frequency (500Hz, 1KHz, 10KHz)? Is this value defined as INC_FREQ? Is the accumulator variable 32 bits? If not, you need to set ACC_BITS to the number of bits. Is it unsigned (This algorithm works because of wraparound)? Is ACC_VALUES 2^(ACC_BITS)? If SAMPLE_BITS is 8, do you have a sine wave with 2^8 (256) values?

If you still can't get it to work, post what you have and I'll be happy to take a look.

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