• Create Account

## N64 Quality Water...

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

14 replies to this topic

### #1projectionmatrix  Members

Posted 12 October 2011 - 01:20 PM

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?

### #2Katie  Members

Posted 13 October 2011 - 04:08 AM

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.

### #3projectionmatrix  Members

Posted 13 October 2011 - 08:30 AM

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 Gerstner Waves 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!

EDIT:
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?

### #4BattleMetalChris  Members

Posted 13 October 2011 - 10:14 AM

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?

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 and y. To combine waves in different directions, just add them together.

### #5projectionmatrix  Members

Posted 13 October 2011 - 12:57 PM

BattleMetalChri,

Thanks for the reply. Works perfectly now!

### #6projectionmatrix  Members

Posted 14 October 2011 - 09:10 AM

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?

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];
}
}
}

### #7mikiex  Members

Posted 15 October 2011 - 02:33 AM

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.

In the shader? that was on N64!

Would be interested to see some screenshots of your results PrjM

### #8MortenB  Members

Posted 15 October 2011 - 05:45 AM

Here's a description of what we used to do back in the old days.
http://freespace.virgin.net/hugo.elias/graphics/x_water.htm
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.

### #9whitechaos35  Members

Posted 15 October 2011 - 06:41 PM

Can anyone point me in the right direction to using a lookup table for this?

Take a look at Direct Digital Synthesis.

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:

#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

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


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. ;)

### #10projectionmatrix  Members

Posted 16 October 2011 - 11:28 AM

Wow, just wow! Thanks sooo much @whitechaos35 i would have never found that on my own!

### #11projectionmatrix  Members

Posted 17 October 2011 - 08:10 AM

@mikiex
Screenshots don't demonstrate the water well, here are the files i have so far WinMain.cpp GLWater.h GLWater.cpp

@whitechaos35
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)

@MortenB
I'm going to be looking into the link you posted; i will need ripples around objects eventually.

THANKS EVERYONE!

### #12Uthman  Members

Posted 17 October 2011 - 08:35 AM

Here's a description of what we used to do back in the old days.
http://freespace.vir...ics/x_water.htm
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.

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.

"a low level aho master like you couldn't kill me even if I let you"

### #13projectionmatrix  Members

Posted 17 October 2011 - 10:04 AM

I found a solution (In some old nVidia paper), so far this is acceptable for what i'm doing.
Also: Updated GLWater.cpp

#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 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)
}

float FAST_COS(float n) {
float f = n * HALF_MAX_CIRCLE_ANGLE * PI_RECIPRICAL;
int i = int(f);
if (i < 0)
}

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);
}
}

### #14projectionmatrix  Members

Posted 17 October 2011 - 12:41 PM

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

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];
}
}
}

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!

### #15whitechaos35  Members

Posted 22 October 2011 - 04:45 PM

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!

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.