Sign in to follow this  

Heightmap loading weirdness

This topic is 4861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have some code reading heightmap data from an image. The image gets loaded into memory and then I read the color from it. However, I'm having some trouble getting it to work as I should. I had some code that I have used for a while, but when I try a different sized heightmap, it seems to break. Here's what I had:
height = (p[(z*Image->w + x)*Image->format->BytesPerPixel + 2*z] - 128) * vScaleIn;
I think that's fairly standard, except for the "+ 2*z" part. I had to do that to get it to load this image right. However, if I resize the heightmap, I need to use this (no "+ 2*z"):
height = (p[(z*Image->w + x)*Image->format->BytesPerPixel] - 128) * vScaleIn;
If I use the smaller heightmap with the first code example, it looks like this. If I use the second (what seems like the correct code) with the smaller heightmap, it looks like this. If I use the larger heightmap with the first code, it looks like this. I took that image at a different location, but it shows the same error, just in the opposite direction. The larger heightmap looks correct with the second code bit. I'm not sure why that particular image needs the "+ 2*z". I've tried resaving the image, but it doesn't seem to be a problem with the file itself. Any ideas what I need to do to get code that works for any heightmap?

Share this post


Link to post
Share on other sites
Looks like a stide issue. Are you sure the bytes per pixel are correct? I mean the way you have it slanted out seems to suggest that your stride is off.

What format is your heightmap in?

~Wave

Share this post


Link to post
Share on other sites
Yeah, stride does seem like a likely candidate. However, both bytes per pixel values are set to 3. I'm using SDL_image to load the images, so it figures that out and I assume it does it right. My images are png images, and I have saved them both from the gimp. The larger one is just a resize of the the smaller one, so they shouldn't be all that different. I tried using "(Image->format->BytesPerPixel+1)" (perhaps allowing for an alpha channel), but that just results in a seg fault.

Share this post


Link to post
Share on other sites
To access a SDL_Surface you should not use the image width in the computation. Instead use the length of scanline given by the surface member "pitch".

Share this post


Link to post
Share on other sites

This topic is 4861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this