Erosion 5

From DaveWiki

Jump to: navigation, search

This example changes the type of blend applied to the erosion to result in an even better looking result. This blend type uses an exponential decay which can be applied to an arbitrarily large radius. Override the CErosionSample::ErodePoint() method again along with some new constants:

#include "erosion4.h"
 
class CErosionSample5 : public CErosionSample4
{
public:
 
		/* Constants for controlling the new erosion blend */
	int   BLEND_RADIUS;
	float BLEND_FALLOFF;
	int   BLEND_SIZE;
 
	/*
	 * Class Constructor 
	 */
	CErosionSample5()
	{
		BLEND_RADIUS  = 10;
		BLEND_FALLOFF = 0.5f;
		BLEND_SIZE    = BLEND_RADIUS * 2 + 1;
	}
 
	/*
	 * A more complex erosion blend algorithm.
	 */
	virtual void ErodePoint (const int X, const int Y)
	{
		int   X1, Y1;
		float Distance;
		float HeightDelta;
 
		for (Y1 = Y - BLEND_RADIUS; Y1 <= Y + BLEND_RADIUS; ++Y1)
		{
			for (X1 = X - BLEND_RADIUS; X1 <= X + BLEND_RADIUS; ++X1)
			{
				Distance = sqrt((float)((Y1 - Y)*(Y1 - Y) + (X1 - X)*(X1 - X)));
				if (Distance > BLEND_RADIUS) continue;
				HeightDelta = pow(BLEND_FALLOFF, Distance) * EROSION_DELTA;
 
				m_HeightMap.SetValue (X1, Y1, m_HeightMap.GetValue(X1,Y1)  - HeightDelta);
				m_ErosionMap.SetValue(X1, Y1, m_ErosionMap.GetValue(X1,Y1) - HeightDelta);
			}
		}
 
	}
 
};
 
int _tmain(int argc, _TCHAR* argv[])
{
   	CErosionSample5 ErosionSample;
 
	ErosionSample.CreateBaseNoise();
	ErosionSample.ErodeAll();
 
	davelib::OutputHeightMapGrayScale("erosion5a.bmp", ErosionSample.m_HeightMap);
	davelib::OutputHeightMapGrayScale("erosion5b.bmp", ErosionSample.m_ErosionMap);
 
   	return 0;
}

This type results more finer blend results:
Image:Erosion5.jpg Image:Erosion5a.jpg
One important note about this method is that it is considerably slower than the previous erosion methods. While all the previous versions took a few seconds while this one takes close to 40 secs with a BLEND_RADIUS of 10 (smaller radii will decrease this).

Personal tools