This code something about to make terrian for RTS game that programming use c++ did somebody can tell me How to understand
CosInterpolate function the book say that is noise function I don't
clear understand noise function did somebody can told to me
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, i);
float amp = pow(persistence, i);
//Calculate the x,y noise coordinates
float tx = xf * freq;
float ty = yf * freq;
int tx_int = tx;
int ty_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 = 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;
}
lidongheng