// --------------------------------------------------------------------
void Landscape::UpdateHeightmap(Brush &AffectingBrush)
{
    vec2 CurrentVec;
    vec2 BrushPosition = AffectingBrush.GetPosition();
    float BrushRadius = AffectingBrush.GetRadius();
    float Distance, DistanceFactor;
    float HeightDifference;
    float HeightAverage = 0, HeightSum = 0;
    int Counter = 0;

    if (AffectingBrush.GetMode())
    {
        for (unsigned int x = 0; x < ClipmapVBOWidth; x++)
        {
            for (unsigned int z = 0; z < ClipmapVBOWidth; z++)
            {
                CurrentVec.x = ClipmapVBOData[2 * (z * ClipmapVBOWidth + x)];
                CurrentVec.y = ClipmapVBOData[2 * (z * ClipmapVBOWidth + x) + 1];

                Distance = distance(CurrentVec, BrushPosition);
                
                if (Distance <= BrushRadius)
                {
                    Counter++;
                    HeightSum += HeightData[z * ClipmapVBOWidth + x];
                }
            }
        }

        HeightAverage = HeightSum / Counter;
    }

    for (unsigned int x = 0; x < ClipmapVBOWidth; x++)
    {
        for (unsigned int z = 0; z < ClipmapVBOWidth; z++)
        {
            CurrentVec.x = ClipmapVBOData[2 * (z * ClipmapVBOWidth + x)];
            CurrentVec.y = ClipmapVBOData[2 * (z * ClipmapVBOWidth + x) + 1];

            Distance = distance(CurrentVec, BrushPosition);

            if (Distance <= BrushRadius)
            {
                DistanceFactor = 1.0f - Distance / BrushRadius;

                switch (AffectingBrush.GetMode())
                {
                case 0:           
                    HeightData[z * ClipmapVBOWidth + x] += 0.15f * ((DistanceFactor < 0.5f) ? (pow(DistanceFactor, 2)) : (0.5f - pow(1.0f - DistanceFactor, 2))); 
                    break;
                case 1:
                    HeightData[z * ClipmapVBOWidth + x] -= 0.15f * ((DistanceFactor < 0.5f) ? (pow(DistanceFactor, 2)) : (0.5f - pow(1.0f - DistanceFactor, 2))); 
                    break;
                case 2:
                    HeightDifference = HeightAverage - HeightData[z * ClipmapVBOWidth + x];
                    HeightData[z * ClipmapVBOWidth + x] += 0.02f * HeightDifference;
                    break;
                case 3:
                    HeightData[z * ClipmapVBOWidth + x] += 0.15f * ((DistanceFactor < 0.5f) ? (pow(DistanceFactor, 2)) : (pow(1.0f - DistanceFactor, 2))); 
                    break;
                }
            }   
        }
    }
}