Game Development Reference
In-Depth Information
Note: You do not have to use the RAW format to store your height
information; you can use any format that meets your needs. The RAW
format is just one example of a format that we can use. We decided to
use the RAW format because many popular image editors can export
to that format and it is very easy to read the data in a RAW file into the
application. The samples in this chapter use 8-bit RAW files.
13.1.2 Loading a RAW File
Since a RAW file is nothing more than a contiguous block of bytes, we
can easily read in the block with this next method. Note that the vari-
able _heightmap is a member of the Terrain class and defined as:
std::vector<int> _heightmap;
bool Terrain::readRawFile(std::string fileName)
{
// A height for each vertex
std::vector<BYTE> in( _numVertices );
std::ifstream inFile(fileName.c_str(), std::ios_base::binary);
if( inFile == 0 )
return false;
inFile.read(
(char*)&in[0], // buffer
in.size());// number of bytes to read into buffer
inFile.close();
// copy BYTE vector to int vector
_heightmap.resize( _numVertices );
for(inti=0;i<in.size(); i++)
_heightmap[i] = in[i];
return true;
}
Observe that we copy the vector of bytes to a vector of integers; we do
this so that we can scale the height values outside the [0, 255] interval.
The only restriction of this method is that the RAW file being read
in must have at least as many bytes as there are vertices in the terrain.
Therefore, if you are reading in a 256x256 RAW file, you must con-
struct the terrain with, at most, 256x256 vertices.
13.1.3 Accessing and Modifying the Heightmap
The Terrain class provides the following two methods to access and
modify an entry in the heightmap:
int Terrain::getHeightmapEntry(int row, int col)
{
return _heightmap[row * _numVertsPerRow + col];
Search WWH ::




Custom Search