I am rendering terrain using a quad-tree system. I need to split a height-map into four sections using the method splitHeightmap(float[] originalMap, int quadrant)
with the quadrant being a number from 0-3. The map needs to be split into quarters so if 0 is passed as the quadrant, the bottom left quarter of the array is returned as a new float array. I have a little base code, but I'm not sure how to actually sample the map depending on the desired quadrant:
protected float[] splitHeightMap(float[] heightMap, TerrainQuadrant quadrant) {
float[] newHeightMap = new float[size >> 1];
int newSize = size >> 1;
for (int i = 0; i < newSize; i++)
for (int j = 0; j < newSize; j++)
newHeightMap[i * newSize + j] = sampleHeightAt(heightMap, i, j);
return newHeightMap;
}
protected float sampleHeightAt(float[] heightMap, int x, int z) {
return heightMap[z + x * size];
}
Edit: I have written what I think should work, but am getting a ArrayIndexOutOfBoundsException for index 65792 (with a original heightmap of 512x512):
protected float[] splitHeightMap(float[] heightMap, TerrainQuadrant quadrant) {
float[] newHeightMap = new float[(size >> 1) * (size >> 1)];
int newSize = size >> 1;
int xOffset = 0, zOffset = 0;
int xCount = 0, yCount = 0;
switch (quadrant) {
case BottomRight:
xOffset = newSize;
break;
case TopLeft:
zOffset = newSize;
break;
case TopRight:
xOffset = newSize;
zOffset = newSize;
break;
default:
break;
}
for (int x = xOffset; x < xOffset + newSize; x++)
for (int z = zOffset; z < zOffset + newSize; z++) {
newHeightMap[xCount + yCount * newSize] = heightMap[z + x * size]; // should this be 'z + x * size' or 'x + z * size'?
xCount++;
yCount++;
}
return newHeightMap;
}