void Field::Generate(float fLeftBottom, float fLeftTop, float fRightTop, float fRightBottom) { initialize(); float fMax; float fMin; vectPoints[0][0] = fLeftBottom; vectPoints[0][unSize-1] = fRightBottom; vectPoints[unSize-1][0] = fLeftTop; vectPoints[unSize-1][unSize-1] = fRightTop; unsigned int unSpacing = unSize - 1; while (unSpacing > 1) { int unHalfSpacing = unSpacing / 2; for (unsigned int unI = unHalfSpacing; unI < unSize; unI += unSpacing) { for (unsigned int unJ = unHalfSpacing; unJ < unSize ; unJ += unSpacing ) { vectPoints[unI][unJ] = DiamondStep(unI, unJ, unHalfSpacing ) + randomize() * unSpacing * fVariability; } } for (unsigned int unI = 0; unI < unSize; unI += unHalfSpacing) { unsigned int unJStart = ((unI/unHalfSpacing) % 2 == 0 ) ? unHalfSpacing : 0; for (unsigned int unJ = unJStart; unJ < unSize ; unJ += unSpacing ) { vectPoints[unI][unJ] = SquareStep(unI, unJ, unHalfSpacing ) + randomize() * unSpacing * fVariability; } } unSpacing = unHalfSpacing; } fMax = vectPoints[0][0]; fMin = vectPoints[0][0]; for (unsigned int unI = 0; unI < (unSize-1); unI++ ) for (unsigned int unJ = 0; unJ < (unSize-1); unJ++ ) { if(vectPoints[unI][unJ] > fMax) { fMax = vectPoints[unI][unJ]; } if(vectPoints[unI][unJ] < fMin) { fMin = vectPoints[unI][unJ]; } } }
/* Iterates through the terrain mesh applying the diamond sqare algorithm until all the squares have been processed */ void DiamondSquare::Generate() { int stepSize = (int)terrainSize; // Starting with the whole mesh, apply the diamond square then cut the mesh in half and repeat // until there are no more subsquares to process while(stepSize > 0) { // Apply diamond square algorithm to each sub sqaure for(int i = 0; i < terrainSize; i += stepSize) { for(int j = 0; j < terrainSize; j += stepSize) { DiamondStep(i, j, stepSize, params->roughness); SquareStep(i, j, stepSize, params->roughness); } } // Reduce the amount of roughness as the sqaures get smaller to prevent very spiky terrain params->roughness *= params->roughnessDecrement; // cut the mesh in half stepSize /= 2; } }