Heaghtmapping, reading from *.RAW

This is a discussion on Heaghtmapping, reading from *.RAW within the Game Programming forums, part of the General Programming Boards category; ok, I have a question, I am learning height mapping, starting with the basic .RAW file read. How does it ...

  1. #1
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790

    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.
    Last edited by EvBladeRunnervE; 03-27-2003 at 03:14 PM.

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    i do height mapping with .bmp's and use color values. im not sure how it works with .raw, never tried.

  3. #3
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    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.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    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.
    Last edited by Eibro; 03-27-2003 at 05:40 PM.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    EvBladerunner
    Guest
    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. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Magos
    Everytime new fails for me it returns NULL, and I've never used that (nothrow) stuff...
    What compiler?

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Eibro
    What compiler?
    Borland & Visual C/C++
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Magos
    Borland & Visual C/C++
    If it's VC6 it doesn't support new(nothrow)

  10. #10
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Originally posted by EvBladerunner
    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?
    yes, that's the idea. the value read in gets mapped to the terrains y value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2009, 02:14 AM
  2. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  3. Resetting a ifstream object after reading the whole file
    By Zeeshan in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2008, 08:03 AM
  4. Replies: 2
    Last Post: 01-28-2008, 02:07 AM
  5. Fun with reading hex
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 02-17-2006, 05:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21