/** * Initialize the 4 corners of the patch. * * @param randRange The current random range. * @param modifier The height modifier. * @param random The random. */ private void initCorners(float randRange, HeightModifier modifier, Random random) { if (modifier.isModifiable(0, 0)) { modifier.set(0, 0, getRandomValue(randRange, random)); } else { modifier.copyUnModifiable(0, 0); } if (modifier.isModifiable(0, mPatchRowsCols)) { modifier.set(0, mPatchRowsCols, getRandomValue(randRange, random)); } else { modifier.copyUnModifiable(0, mPatchRowsCols); } if (modifier.isModifiable(mPatchRowsCols, 0)) { modifier.set(mPatchRowsCols, 0, getRandomValue(randRange, random)); } else { modifier.copyUnModifiable(mPatchRowsCols, 0); } if (modifier.isModifiable(mPatchRowsCols, mPatchRowsCols)) { modifier.set(mPatchRowsCols, mPatchRowsCols, getRandomValue(randRange, random)); } else { modifier.copyUnModifiable(mPatchRowsCols, mPatchRowsCols); } }
/** * Create a "square" structure out of each diamond. This is done by adding a new value in the * middle of the long-side of each diamond. * * @param iteration The iteration number. * @param randRange The current random range. * @param random The random. * @param modifier The height modifier. */ private void square(int iteration, float randRange, Random random, HeightModifier modifier) { final int stepSize = mPatchRowsCols >> iteration; final int stepSizeH = stepSize / 2; boolean toggle = true; for (int z = 0; z < mPatchSideVertices; z += stepSizeH) { final boolean hasTop = z < (mPatchSideVertices - 1); final boolean hasBottom = z > 0; int x = toggle ? stepSizeH : 0; toggle = !toggle; for (; x < mPatchSideVertices; x += stepSize) { if (modifier.isModifiable(x, z)) { final boolean hasRight = x > 0; final boolean hasLeft = x < (mPatchSideVertices - 1); int count = 0; float value = 0; if (hasRight) { count++; value += modifier.get(x - stepSizeH, z); } if (hasLeft) { count++; value += modifier.get(x + stepSizeH, z); } if (hasBottom) { count++; value += modifier.get(x, z - stepSizeH); } if (hasTop) { count++; value += modifier.get(x, z + stepSizeH); } modifier.set(x, z, (value / count) + getRandomValue(randRange, random)); } else { modifier.copyUnModifiable(x, z); } } } }