Exemple #1
0
HRESULT HEIGHTMAP::CreateRandomHeightMap(int seed, float noiseSize, float persistence, int octaves)
{
	//For each map node
	for(int y=0;y<m_size.y;y++)
		for(int x=0;x<m_size.x;x++)
		{
			//Scale x & y to the range of 0.0 - m_size
			float xf = ((float)x / (float)m_size.x) * noiseSize;
			float yf = ((float)y / (float)m_size.y) * noiseSize;

			float total = 0;			

			// For each octave
			for(int i=0;i<octaves;i++)
			{
				//Calculate frequency and amplitude (different for each octave)
				float freq = pow(2.0f, i);
				float amp = pow(persistence, i);

				//Calculate the x,y noise coordinates
				float tx = xf * freq;
				float ty = yf * freq;
				int tx_int = (int)tx;
				int ty_int = (int)ty;

				//Calculate the fractions of x & y
			    float fracX = tx - tx_int;
			    float fracY = ty - ty_int;

				//Get the noise of this octave for each of these 4 points
				float v1 = Noise(tx_int + ty_int * 57 + seed);
				float v2 = Noise(tx_int+ 1 + ty_int * 57 + seed);
				float v3 = Noise(tx_int + (ty_int+1) * 57 + seed);
				float v4 = Noise(tx_int + 1 + (ty_int+1) * 57 + seed);

				//Smooth in the X-axis
				float i1 = CosInterpolate(v1 , v2 , fracX);
				float i2 = CosInterpolate(v3 , v4 , fracX);

				//Smooth in the Y-axis
				total += CosInterpolate(i1 , i2 , fracY) * amp;
			}

			int b = (int)(128 + total * 128.0f);
			if(b < 0)b = 0;
			if(b > 255)b = 255;

			//Save to heightMap
			m_pHeightMap[x + y * m_size.x] = (b / 255.0f) * m_maxHeight;
		}

	return S_OK;
}
float CSulPerlinNoise2D::InterpolatedNoise( float x, float y, float freq )
{
	sigma::int32 integer_X    = int(x);
	float fractional_X = x - integer_X;

	sigma::int32 integer_Y    = int(y);
	float fractional_Y = y - integer_Y;

	float v1 = IntNoiseLoop( integer_X,		integer_Y, freq );
	float v2 = IntNoiseLoop( integer_X+1,	integer_Y, freq );
	float v3 = IntNoiseLoop( integer_X,		integer_Y+1, freq );
	float v4 = IntNoiseLoop( integer_X+1,	integer_Y+1, freq );

	float i1= CosInterpolate( v1, v2, fractional_X );
	float i2= CosInterpolate( v3, v4, fractional_X );	  

	return  CosInterpolate( i1 ,i2 ,fractional_Y );	  
}
HRESULT HEIGHTMAP::CreateRandomHeightMap(int seed, float noiseSize, float persistence, int octaves)
{
	if(m_pHeightMapTexture != NULL){m_pHeightMapTexture->Release(); m_pHeightMapTexture = NULL;}
	m_pDevice->CreateTexture(m_size.x, m_size.y, 1, D3DUSAGE_DYNAMIC, D3DFMT_L8, D3DPOOL_DEFAULT, &m_pHeightMapTexture, NULL);

	D3DLOCKED_RECT lock;
	m_pHeightMapTexture->LockRect(0, &lock, NULL, NULL);

	//For each map node
	for(int y=0;y<m_size.y;y++)
		for(int x=0;x<m_size.x;x++)
		{
			//Scale x & y to the range of 0.0 - m_size
			float xf = ((float)x / (float)m_size.x) * noiseSize;
			float yf = ((float)y / (float)m_size.y) * noiseSize;

			float total = 0;			

			// For each octave
			for(int i=0;i<octaves;i++)
			{
				//Calculate frequency and amplitude (different for each octave)
				float freq = pow(2.0f, i);
				float amp = pow(persistence, i);

				//Calculate the x,y noise coordinates
				float tx = xf * freq;
				float ty = yf * freq;
				int tx_int = (int)tx;
				int ty_int = (int)ty;

				//Calculate the fractions of x & y
			    float fracX = tx - tx_int;
			    float fracY = ty - ty_int;

				//Get the noise of this octave for each of these 4 points
				float v1 = Noise(tx_int + ty_int * 57 + seed);
				float v2 = Noise(tx_int+ 1 + ty_int * 57 + seed);
				float v3 = Noise(tx_int + (ty_int+1) * 57 + seed);
				float v4 = Noise(tx_int + 1 + (ty_int+1) * 57 + seed);

				//Smooth in the X-axis
				float i1 = CosInterpolate(v1 , v2 , fracX);
				float i2 = CosInterpolate(v3 , v4 , fracX);

				//Smooth in the Y-axis
				total += CosInterpolate(i1 , i2 , fracY) * amp;
			}

			int b = (int)(128 + total * 128.0f);
			if(b < 0)b = 0;
			if(b > 255)b = 255;

			BYTE *bDest = (BYTE*)lock.pBits;
			bDest += y * lock.Pitch + x;
			*bDest = b;

			//Save to heightMap
			m_pHeightMap[x + y * m_size.x] = ((float)b / 255.0f) * m_maxHeight;
		}

	m_pHeightMapTexture->UnlockRect(0);

	return S_OK;
}