Creating Quads from Heightmaps
You're now ready to create a quad with varying height. Because you need lots of them for realistic terrains, the challenge is how to convert heightmaps into quads. Figure 4 is a white grid superimposed on the previous heightmap. Looking at each piece of the grid, you see that the rectangular grid sector is another heightmap but smaller. If you create a high-resolution grid, you realize that the grid sectors become very small and thus easy to approximate with a single quad. In other words, to approximate a heightmap, just split it into many small parts, each representing a quad.
To create a quad:
- Divide the image into many small sectors (a minimum size of 2×2 pixels).
- Take each sector's corner pixels and read their values (0-255).
- Assign these values as the heights of the quad (see method declaration).
After creating the quads from a heightmap, render them one after another and you have a heightmap. As the resolution of the heightmap grid increases, so does the smoothness of the terrain, as you use more quads to represent the terrain. However, you are also drastically increasing the memory footprint and increasing the number of polygons that the GPU has to push. This is a trade-off that needs to be done on every mobile phone depending on available memory, GPU power, and so on.