Thread: Is it a good method (BMP heightmap to raw) ?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    14

    Is it a good method (BMP heightmap to raw) ?

    Hi Everyone,

    I'm trying to model a terrain (Puget sound) by using its Heightmap Model : Puget Sound
    To read that PNG file in my program I used this function
    Code:
    bool CTerrain::LoadHeightMap(char* fileName, int length, int width)
    {
    	FILE *file;
    	file = fopen(fileName, "rb");
    
    	//check to see if there is data
    	if (file == NULL)
    		return false;
    
    	BYTE  Heights = ( BYTE *)malloc(length*width*sizeof( BYTE ));
    	fread(Heights,sizeof( BYTE ),length*width,file);
    	fclose(file);
    
    	////check to see if there is data
    	if( Heights != NULL )
    	{
    		return true;
    	}
    	else 
    		return false; 
    }
    But unfortunately the results is completely disaster. So, I convert PNG file to RAW using photoshop (read PNG file in photoshop than exported as RAW file) and I used the same function to read that RAW file and the result was good.

    My question is : Is the method that I used correct ?

    Thanks in advance
    Last edited by fleurdelys77; 09-07-2011 at 09:03 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > My question is : Is the method that I used correct ?
    No.

    A BMP file has a lot more information and structure than a simple RAW file (w*h*pixels).
    Look up "BMP file format" in any friendly search engine.

    > if( Heights != NULL )
    You must do this BEFORE trying to fread all over it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    14
    Thanks Salem for your reply. (Sorry it's PNG not BMP) But what should I do to read the Height value from this PNG heightmap (sorry again I'm novice )

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well to get pixel data out of a PNG file, your best bet is to use libpng Home Page
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You could simply load the PNG into photoshop or paint shop and convert it to RAW instead of trying to load the PNG. Note that if the scale of your terrain cells do not match the scale of the terrain in the image then you will get strange results. Normally you will take the values in the heightmap and multiply them by a scaling factor to get better results for your 3D terrain. I normally multiply by 256 or more to get a good result and often I translate the raw data so that 128 in the raw map equates to y = 0.0f in the world. This allows for a much greater scale and flexibility in 3D.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    14
    Quote Originally Posted by VirtualAce View Post
    You could simply load the PNG into photoshop or paint shop and convert it to RAW instead of trying to load the PNG.
    Do you mean that the method that I used is correct ?

    Quote Originally Posted by VirtualAce View Post
    Note that if the scale of your terrain cells do not match the scale of the terrain in the image then you will get strange results. Normally you will take the values in the heightmap and multiply them by a scaling factor to get better results for your 3D terrain. I normally multiply by 256 or more to get a good result and often I translate the raw data so that 128 in the raw map equates to y = 0.0f in the world. This allows for a much greater scale and flexibility in 3D.
    Actually the strange reasult I get when I load PNG not RAW. Is that make any sens ?
    Thanks in advance VirtualAce

    Thank you salem I'll take a look to libpng

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by fleurdelys77 View Post
    Do you mean that the method that I used is correct ?
    It is one possible approach. Obviously if you just want to play around with a single file it is perfectly fine to manually convert it to a format that you are comfortable with. On the other hand, if you are looking to load arbitrary files in a fixed (say, PNG) format, it would be cumbersome to have to manually convert them to RAW format.

    The alternative would be to load the PNG directly and extract the height data programmatically. As Salem pointed out, libpng would be your best bet.

    You see, the difference between a RAW file and established picture file formats, is that the latter include additional information on the file, such as width, height, number of bits per pixel, etc., and may store the actual pixel data in a compressed form, while the RAW file only includes the raw pixel data, that's why your method of loading the height data works on RAW, but not on PNG files.

    Actually the strange reasult I get when I load PNG not RAW. Is that make any sens ?
    The strange results that VirtualAce is referring to, is that the heightmap may look excessively distorted, because the width and height of a terrain cell (basically corresponding to one pixel in the file) may not scale properly with the height. E.g. standard 8-bit integer height values would give you anything from 0 to 255 in height, which, when naively rendered on a 1 x 1 cell, would give you an immense height compared on a very small area. You would typically increase the size of the cell to a more reasonable value such as 256 x 256 units.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    14
    thank you so much MWAAAHAAA

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Displaying a heightmap
    By Dark_Phoenix in forum Game Programming
    Replies: 6
    Last Post: 11-11-2006, 04:24 PM
  2. Need a good method
    By Asagohan in forum Tech Board
    Replies: 11
    Last Post: 04-08-2005, 04:57 PM
  3. is tha good method?
    By MartinLiao in forum C Programming
    Replies: 27
    Last Post: 07-25-2004, 06:34 PM
  4. Is this a good framerate method?
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 07-08-2004, 02:42 AM
  5. Good sound storage method
    By VirtualAce in forum Game Programming
    Replies: 1
    Last Post: 10-14-2003, 05:18 AM