1. ## Heaghtmapping, reading from *.RAW

ok, I have a question, I am learning height mapping, starting with the basic .RAW file read. How does it exactly determine the height of this pixel in space? does it read in the number of "bits" the color(white->shades of gray->black), or what?

EDIT: the reason I am wondering is that all I do is read the file into an array, then read from the array in the height mapping algorithm
:end Edit
P.S. I am finding heightmapping is alot easier than I thought it would be, I had tried it before, but kept messing up on my algorithms.

2. i do height mapping with .bmp's and use color values. im not sure how it works with .raw, never tried.

3. 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.

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

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.

4. heightMap= new unsigned char [width*height];
if(heightMap==NULL)
Might want to change that to heightMap = new(nothrow) unsigned char[width*height];
new throws an exception of type std::bad_alloc, if you want NULL returned on failure, use new(nothrow)

if(heightMap) delete[] heightMap;
No need for the if statement. delete or delete[]'ing a NULL pointer has no effect. So set heightMap to be initially NULL, then delete[] it at the end of your function.

5. Originally posted by Eibro
Might want to change that to heightMap = new(nothrow) unsigned char[width*height];
new throws an exception of type std::bad_alloc, if you want NULL returned on failure, use new(nothrow)
Everytime new fails for me it returns NULL, and I've never used that (nothrow) stuff...

6. so basically, with heightmapping, it is just returning the intensity of the byte it is currently reading(0-255) and returning it to the vertice's y plane position?

7. Originally posted by Magos
Everytime new fails for me it returns NULL, and I've never used that (nothrow) stuff...
What compiler?

8. Originally posted by Eibro
What compiler?
Borland & Visual C/C++

9. Originally posted by Magos
Borland & Visual C/C++
If it's VC6 it doesn't support new(nothrow)