Loading a heightmap

BlitzMax Forums/BlitzMax Beginners Area/Loading a heightmap

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I had allready looked into your HeightMap Toolbox you posted in the Code Archives, but I didn't really understand the code I found there


Heh. The intention with that code was to create a ‘class’ that could just be plugged-in / imported into an existing project. I suspect I’ve failed. Possibly I need to provide a collection of simple, old-fashioned functions instead.

As for those two lines of code, that I ripped from my Heightmap Toolbox written over a year ago, on closer analysis I think I must have been drunk at the time!

Here's a simpler method:

   	For Local x:Int = 0 To (Width - 1)
   		For Local z:Int = 0 To (Height - 1)
      		      Local c:Int = ReadPixel(pm, x, z)
      		      Map[x, z] = c & 255
   		Next
   	Next 


Variable c is a 32 bit signed integer (or, more importantly, 4 bytes), used to store a pixel’s properties: Red, Green, Blue, Alpha. Each byte in the integer corresponds to one of those properties, meaning they each can range in value from 0 to 255.

In order to extract only a single pixel property we use Bitwise AND (&). To get the value of the last byte in the integer, we & c with decimal 255.

Example:

In the sample image, test.png, coordinate 90, 100 has a decimal pixel value of:
16645629.

Decimal:
16645629
AND 255
= 253 (the height value we’re after)

Binary:
00000000111111011111110111111101
00000000000000000000000111111111
-----------------------------------------------
00000000000000000000000111111101

If you need an explanation of bitwise operations this article might help.


EASIER STILL:

Since I’m only using the final byte to store the height value, I could exploit BlitzMax’s Int to Byte type casting behaviour. Change the import loop to:

   	For Local x:Int = 0 To (Width - 1)
   		For Local z:Int = 0 To (Height - 1)
      			Map[x, z] = ReadPixel(pm, x, z)
   		Next
   	Next


Since each Map element is of type Byte, any integer assigned there will have its first three bytes discarded during the cast.