Ohhh yey, fun for me.
Ok heres how the Raw file works. Every pixel in the file (grey value) is listed by one CHAR. So if you have a 128 by 128 height map, your going to have 128*128 characters. Heres the kicker, each character is a value between 1 and 255. Go figure! So if say you have a valley with a grey value of 32, then in the RAW file there will be a ' '(SPACE). If you have a hill with a pure white top (255) there will be a _ to represent it.
Ok, so your asking: Ok how do i USE this information..
Well, you simply read the file (in binary mode) in a for loop going through your vertex arrays or lists or what ever, setting the height value to current character. If you want varying heights that arnt between 0 and 255, you can use a "scaler" value to adjust the height values. Say you want a max terrain height of say 427.
Code:
vertices_array[n].Y = (RawFileArray[n]/255)*427;
What you did, is divide the integer value of the charater by its max value thust getting a PERCENTANGE of height. Multiply the ratio to your max height, and voilla, every thing is scaled to your prefered height.
Heres actuall code I use:
Code:
bool Terrain::Init(char* heightMapFilename, int width, int height, float heightDivider, float scale, float texScale)
{
FILE* file;
unsigned char* heightMap;
m_vertices = new sVERTEX*[height];
for( int r = 0; r < height; r++ )
m_vertices[r] = new sVERTEX[width];
m_rows = width;
m_cols = height;
m_width = width;
m_height = height;
m_scale = 1.0f;
m_pScale = scale;
m_isProc = true;
m_maxDist = 10.0f;
m_texScale = texScale;
float tdist = (m_width/m_cols)*m_pScale;
float tdist2 = (m_height/m_rows)*m_pScale;
file= fopen(heightMapFilename, "rb");
if(file==NULL)
return false;
heightMap= new unsigned char [width*height];
if(heightMap==NULL)
return false; // didnt allocate
fread(heightMap, 1, width*height, file);
for( r = 0; r < m_rows; r++ ){
for( int c = 0; c < m_cols; c++ ) {
m_vertices[r][c].X = r*tdist;
int a = heightMap[(r*height)+c];
m_vertices[r][c].Y = (a/4);
m_vertices[r][c].Z = c*tdist2;
}
}
fclose(file);
if(heightMap)
delete[] heightMap;
return true;
};
I hope this helped.