Sign in to follow this  
ahayweh

2D Water effect woes...

Recommended Posts

Hello all, Has anyone ever coded a successful 2D water effect ala :
Ooooo... Pretty Water
Right now, after much ( to much ) effort, my looks like:
Free Image Hosting at www.ImageShack.us
The left side is the background texture ( attempted to be ) modulated by the water calculations. The right is the actual modulating water. The problem w/the tutorials, is that none of them actual show what the modulating water is supposed to look like. Cause mine looks like it is lacking "smoothness". Here is the offending code:
/*
==========================================================================
LVL_UnderWater_CalcNewWater

Calcuates water.
==========================================================================
*/
void LVL_UnderWater_CalcNewWater( void )
{
	static int xLimit = (currentGame.gfxConfig.xDimen/2 - 1);
	static int yLimit = (currentGame.gfxConfig.yDimen - 1);
	static int i=0;
	int average=0;
	
	
	for( int y=1; y<yLimit; y++ )
	{
		for( int x=1;x<xLimit;x++ )
		{
			average = 
                        ( (_getpixel16( oldWaterBuffer , x + 1 , y ) +
 			   _getpixel16( oldWaterBuffer , x - 1 , y ) +
			   _getpixel16( oldWaterBuffer , x , y + 1  ) +
			   _getpixel16( oldWaterBuffer , x , y - 1 )  + 
						 
			   _getpixel16( oldWaterBuffer , x - 1 , y + 1 )  + 
			   _getpixel16( oldWaterBuffer , x - 1 , y - 1 )  + 
			   _getpixel16( oldWaterBuffer , x + 1 , y + 1 )  + 
			   _getpixel16( oldWaterBuffer , x + 1 , y - 1 )  ) >> 2 ) -						  
						 
			   _getpixel16( newWaterBuffer , x , y );
			
			average -= _getpixel16( newWaterBuffer , x , y )>>5;

			if( average < 0 )
			{
				average = 0;
			}
			if( average > 255 )
			{
				average = 255-average;
				average %= average;
			}


			_putpixel16( newWaterBuffer , x , y , average  );
		}

	}

	i++;
}
/*
===========================================================================
LVL_UnderWater_PaintWater

Simulated dynamic water in offscreen buffer. 
===========================================================================
*/
void LVL_UnderWater_PaintWater( float dampening )
{
	int deltaX=0,deltaY=0,indexX=0,indexY=0,color=0,texture=0;

	for( int y = 87;y < 394;y++ )
	{
		for( int x = 12;x < 304;x++ )
		{
			deltaX = _getpixel16( newWaterBuffer , x , y ) - _getpixel( newWaterBuffer , x + 1 , y );
			deltaY = _getpixel16( newWaterBuffer , x , y ) - _getpixel( newWaterBuffer , x , y + 1 );

			//deltaX >>= 3;
			//deltaY >>= 3;

			indexX = deltaX + x;
			indexY = deltaY + y;

			color = indexX;
			texture = _getpixel16( waterLevel , x , y );
			
			if( color < 0 )
			{
				color = 1;
			}
			if( color > 255 )
			{
				color = 254;
			}
	
			_putpixel16( pageTwo , x , y , color + texture );

			
			_putpixel16( pageThree , x , y , _getpixel16(newWaterBuffer , x ,y ) );
			

			// save rendered water for next frame
			_putpixel16( oldWaterBuffer , x , y , _getpixel16( newWaterBuffer , x , y ) );
			
		}
	}

}

Please excuse the code. After a weekends worth of work, it has degenerated to a hacked-compile-n-hopes-it-works-this-time coding terror. Thanks a lot, ahayweh

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