Sign in to follow this  

writing heightfield to tga

This topic is 4781 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

hello, i want to write out a 2dimensional array to a tga to be used as a heightmap. here is what i got so far:
void SaveHeightMap(char *filename, int width, int height, float hf[1024][1024]) {
	unsigned char *pixels;
	FILE *fp;

	if((fp=fopen(filename, "wb"))==NULL) return;

	unsigned char TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};
	unsigned char header[6] = {width%256,width/256,height%256,height/256,32,0};
	
	pixels = new unsigned char[width*height*4];
	for (int i=0;i<width;i++) {
		for (int j=0;j<height;j++) {
			// here is where i am stuck. ???
		}
	}

	fwrite(TGAheader, sizeof(unsigned char), 12, fp);
	fwrite(header, sizeof(unsigned char), 6, fp);
	fwrite(pixels, sizeof(unsigned char), width*height*4, fp);
	fclose(fp);

}

but i dont know how to write the heightvalues in the heightfield. thanks in advance for any help!

Share this post


Link to post
Share on other sites
you need to find the highest and lowest points in your array of floats (heightfield) then range compress them down to 0-255 and save them as unsigned char's (pixels) its not hard, for each point:

pixels[currentPixel] = (unsigned char)(((hf[currentPixel]-minHeight) / (maxHeight - minHeight)) * 255.0f)


[Edited by - silvermace on November 16, 2004 2:20:28 AM]

Share this post


Link to post
Share on other sites
I haven't done much with terrains, but AFAIK most heightmap images are just grayscale where the intensity of the pixel color corresponds to the height.

I don't know if there's a 'standard' method, but in your case for each pixel r = g = b = (height / heightrange) * 255.0f, and a = whatever, probably 255.

(Note that I assumed minheight = 0. If that's not the case you'd have to tweak the code a little.)

Basically, you normalize the height into the range [0, 1], then multiply that by 255 to scale it into a byte. Then you do the reverse when you read it in.

Does that help?

Share this post


Link to post
Share on other sites
yes, but there must be some error in the TGA writing, because whatever i try, i always get an image that is lightgreen with a black bar at the bottom.

here is what i have now:


void SaveHeightMap(char *filename, int width, int height, float hf[1024][1024]) {
unsigned char *pixels;
FILE *fp;
int p=0;
if((fp=fopen(filename, "wb"))==NULL) return;

unsigned char TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};
unsigned char header[6] = {width%256,width/256,height%256,height/256,32,0};

pixels = new unsigned char[width*height*4];
for (int i=0;i<width;i++) {
for (int j=0;j<height;j++) {
pixels[p]=(unsigned char) (hf[i][j]);
p++;
}
}

fwrite(TGAheader, sizeof(unsigned char), 12, fp);
fwrite(header, sizeof(unsigned char), 6, fp);
fwrite(pixels, sizeof(unsigned char), width*height*4, fp);
fclose(fp);

}




(the hf values are between 0 and 255 already.)

thanks!

edit: ok i figured out, that i have to write each value four times, cause TGA is in BGRA format, right?

Share this post


Link to post
Share on other sites
"edit: ok i figured out, that i have to write each value four times, cause TGA is in BGRA format, right?"

Right :) And actually, the A value probably doesn't matter (although it depends on how the data is going to be read in).

Share this post


Link to post
Share on other sites
ok, so far i manage to get out the right size of tga, but it looks like a binary image:

im doing it like this:

pixels[p++]=(unsigned char) (hf[i][j]);
pixels[p++]=(unsigned char) (hf[i][j]);
pixels[p++]=(unsigned char) (hf[i][j]);
pixels[p++]=(unsigned char) 0;



ok, i got it.
im now writing it with:

putc((int)(hf[i][j),fp);


[Edited by - ehmdjii on November 15, 2004 5:06:51 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
silvermace: you forgot to subtract the minHeight from the height value.

pixels[currentPixel] = (unsigned char)(((hf[currentPixel] - minHeight) / (maxHeight - minHeight)) * 255.0f)

Share this post


Link to post
Share on other sites

This topic is 4781 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