Why are height maps saved in power of 2 dimensions?

Started by
19 comments, last by GameDev.net 18 years, 10 months ago
Shouldn’t height maps be saved in powers of 2 +1? If you have a 64x64 height map and you need to find the midpoint for doing LOD stuff, you are not going to be able to find the vertex midpoint since it divides evenly. I’m asking this because I’m trying to build a practical height map editor( I already built a none practical one).
-----------------------------Download my real time 3D RPG.
Advertisement
Powers of two are used to avoid multiplications.
In an array, instead of doing something like:

array[y][x] or array[y*sizex+sizex]

To access it's values, you can use instead:

array[(y<<shift)sizex]

Which is faster.
Ok, so the last row and column don't really do anything?

edit: ok, nevermind this statement, I'm thinking of power of 2-1.
-----------------------------Download my real time 3D RPG.
Which is faster.

How much faster?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Ok, so how are you going to do a triangle split if you don't have a midpoint? Are you going to have to ignore a whole lot of height map values or round up or down for the midpoint?
-----------------------------Download my real time 3D RPG.
It used to be significantly faster, you'd definitely use powers of two and shifting on a 486 (or clever tricks like storing 8-bit X and Y in the two bytes of a short)... Nowadays your CPU multiplies pretty fast and spends most of its time waiting for the video card anyway so this little optimization won't affect the performance.
Quote:Original post by Fruny
Which is faster.
How much faster?

I guess what you're asking is, "Is it worth it?", implying that it isn't. [smile]

I'de say you're mostly right, only in critical parts of some code will this little trick be of any use nowadays, if at all. And if we're talking about a static array, using this method can even be slower because we'll need to do an extra memory access to read the shift value instead of using an hardcoded one.
Quote:Original post by ManaStone
Shouldn’t height maps be saved in powers of 2 +1? If you have a 64x64 height map and you need to find the midpoint for doing LOD stuff, you are not going to be able to find the vertex midpoint since it divides evenly. I’m asking this because I’m trying to build a practical height map editor( I already built a none practical one).

Wouldn't the midpoint be simply centre of quad formed by 4 actual points of the height map "around" it?

("quad" in the loosest sense, obviously...)
Perhaps this will demonstrate what I am talking about.


-----------------------------Download my real time 3D RPG.
Quote:Original post by ManaStone
Perhaps this will demonstrate what I am talking about.

I understand... what i meant was, 4x4 height map doesn't necessarily mean your geometry made out of it also has to be 4x4 points. Consider the last line in your picture, the one with 5x5 height field -- if you place 4x4 height bitmap over it, you'll see each pixel can 'cleanly' contribute to height of 4 points of the height field 'surrounding' this pixel.

This topic is closed to new replies.

Advertisement