for(int mip = 1 ; mip < MIPMAPLEVELS ; mip++)
{
//the step that this mip takes
int Step = (int)pow(2.0f, mip);
float Delta = 0.0f;
//for each skipped vertice
for(int subY = StartY + Step ; subY <= StartY + CHUNKSIZE ; subY += Step*2)
{
for(int subX = StartX + Step; subX <= StartX + CHUNKSIZE ; subX += Step*2)
{
//skipped center vertices height
float height = m_pHeightMap[subX + subY*m_iRealMapWidth];
//check diagonal
//top left to bottom right
Delta = max( Delta, abs( height - ( m_pHeightMap[(subX-Step) + (subY-Step)*m_iRealMapWidth] + m_pHeightMap[(subX+Step) + (subY+Step)*m_iRealMapWidth])/2.0f));
//top right to bottom left
Delta = max( Delta, abs( height - ( m_pHeightMap[(subX+Step) + (subY-Step)*m_iRealMapWidth] + m_pHeightMap[(subX-Step) + (subY+Step)*m_iRealMapWidth])/2.0f));
//check left node ( Top and bottom )
height = m_pHeightMap[(subX-Step) + subY*m_iRealMapWidth];
Delta = max(Delta, abs(height - (m_pHeightMap[(subX - Step) + (subY - Step)*m_iRealMapWidth] + m_pHeightMap[(subX - Step) + (subY + Step)*m_iRealMapWidth])/2.0f));
//check right node ( Top and Bottom )
height = m_pHeightMap[(subX + Step) + (subY)*m_iRealMapWidth];
Delta = max(Delta, abs( height - (m_pHeightMap[(subX + Step) + (subY - Step)*m_iRealMapWidth] + m_pHeightMap[(subX + Step) + (subY + Step)*m_iRealMapWidth])/2.0f));
//check top node ( Left and Right )
height = m_pHeightMap[subX + (subY - Step)*m_iRealMapWidth];
Delta = max(Delta, abs( height - (m_pHeightMap[(subX-Step) + (subY-Step)*m_iRealMapWidth] + m_pHeightMap[(subX+Step) + (subY-Step)*m_iRealMapWidth])/2.0f));
//check bottom node ( Left and Right)
height = m_pHeightMap[subX + (subY - Step)*m_iRealMapWidth];
Delta = max(Delta, abs( height - (m_pHeightMap[(subX-Step) + (subY+Step)*m_iRealMapWidth] + m_pHeightMap[(subX+Step) + (subY+Step)*m_iRealMapWidth])/2.0f));
}
}
//Dn^2 ( So the distance doesn't need to be square rooted every frame
m_pBlocks[x + y*m_iChunksWidth].minDis[mip] = (Delta*Delta) * ( C * C );
}
for(int mip = 0 ; mip < MIPMAPLEVELS ; mip++)
{
if(dist > m_pBlocks[chunkID].minDis[mip])
m_pBlocks[chunkID].currentLevel = mip;
else
break;
}
if(m_pBlocks[chunkID].currentLevel == MIPMAPLEVELS-1)
{
m_pBlocks[chunkID].interpolationValue = 0.0f;
}
else
{
m_pBlocks[chunkID].interpolationValue = (dist - m_pBlocks[chunkID].minDis[m_pBlocks[chunkID].currentLevel])/(m_pBlocks[chunkID].minDis[m_pBlocks[chunkID].currentLevel+1] - m_pBlocks[chunkID].minDis[m_pBlocks[chunkID].currentLevel]);
}