Skip to main content
GameDev.net gamedev.net
🔒 Locked

Heighmap

Started by Striker47 May 28, 2002 at 4:47 AM 7 replies 1k views
Original Post
Striker47
Striker47
Hiho, I have createt a simple Engine, wich creates a Heigfield Terrain. I got the code from the OpenGL Game Programming Book. But when I trie to load 256*256 Bitmaps I get a error Message. Here is the Code:


#define MAP_X		32
#define MAP_Z		32	

unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
	FILE *filePtr;
	BITMAPFILEHEADER	bitmapFileHeader;
	unsigned char		*bitmapImage;
	int					imageIdx = 0;
	unsigned char		tempRGB;

	filePtr = fopen(filename, "rb");
	if (filePtr == NULL)
		return NULL;

	fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
	
	if (bitmapFileHeader.bfType != BITMAP_ID)
	{
		fclose(filePtr);
		return NULL;
	}

	fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);
	fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
	bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
	if (!bitmapImage)
	{
		free(bitmapImage);
		fclose(filePtr);
		return NULL;
	}

	fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);
	if (bitmapImage == NULL)
	{
		fclose(filePtr);
		return NULL;
	}

	for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3)
	{
		tempRGB = bitmapImage[imageIdx];
		bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
		bitmapImage[imageIdx + 2] = tempRGB;
	}

	fclose(filePtr);
	return bitmapImage;
}

...

void InitializeTerrain()
{
	for (int z = 0; z < MAP_Z; z++)
	{
		for (int x = 0; x < MAP_X; x++)
		{
			terrain[x][z][0] = float(x)*MAP_SCALE;				
			terrain[x][z][1] = (float)imageData[(z*MAP_Z+x)*3];
			terrain[x][z][2] = -float(z)*MAP_SCALE;
		}
	}
}
		

...


void render()
...
	imageData = LoadBitmapFile("terrain2.bmp", &bitmapInfoHeader);
	InitializeTerrain();
...


	glBindTexture(GL_TEXTURE_2D, land);
	for (int z = 0; z < MAP_Z-1; z++)
	{
		glBegin(GL_TRIANGLE_STRIP);
		for (int x = 0; x < MAP_X-1; x++)
		{
			// for each vertex, we calculate the grayscale shade color, 
			// we set the texture coordinate, and we draw the vertex.
			/*
			   the vertices are drawn in this order:

			   0  ---> 1
			          /
				    /
			     |/
			   2  ---> 3
			*/
		
			// draw vertex 0
			glColor3f(terrain[x][z][1]/255.0f, terrain[x][z][1]/255.0f, terrain[x][z][1]/255.0f);
			glTexCoord2f(0.0f, 0.0f);
			glVertex3f(terrain[x][z][0], terrain[x][z][1], terrain[x][z][2]);

			// draw vertex 1
			glTexCoord2f(1.0f, 0.0f);
			glColor3f(terrain[x+1][z][1]/255.0f, terrain[x+1][z][1]/255.0f, terrain[x+1][z][1]/255.0f);
			glVertex3f(terrain[x+1][z][0], terrain[x+1][z][1], terrain[x+1][z][2]);

			// draw vertex 2
			glTexCoord2f(0.0f, 1.0f);
			glColor3f(terrain[x][z+1][1]/255.0f, terrain[x][z+1][1]/255.0f, terrain[x][z+1][1]/255.0f);
			glVertex3f(terrain[x][z+1][0], terrain[x][z+1][1], terrain[x][z+1][2]);

			// draw vertex 3
			glColor3f(terrain[x+1][z+1][1]/255.0f, terrain[x+1][z+1][1]/255.0f, terrain[x+1][z+1][1]/255.0f);
			glTexCoord2f(1.0f, 1.0f);
			glVertex3f(terrain[x+1][z+1][0], terrain[x+1][z+1][1], terrain[x+1][z+1][2]);
		}
		glEnd();
	}
....
}
 
When I set MAP_X and MAP_Z to 256 and load a 256*256 Hiegmap, i get an error message, why? And how to solve this Problem? Thx4All, Striker PS: Sorry for my bad English.
davepermen
davepermen
(float)imageData[(z*MAP_Z+x)*3];
this should be x + z*MAP_X in fact, not MAP_Z (*3 in the end) but as both are the same, it doesn''t really mather..

what you get? a crash or a userdefined errormessage from you, or something else? (if its a messagebox, pressing ctrl-c should copy the text out of it)

"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
masterg
masterg

#define MAP_X 32
#define MAP_Z 32


You seem to be defining the images length and width here to 32, when in fact you are trying to load a 128x128 image!
masterghttp:/masterg.andyc.org
psijgo
psijgo
those definitions define the size of the map, not the individual images (i presume).
masterg
masterg
(I read the book last summer)
masterghttp:/masterg.andyc.org
Striker47
Striker47
When I try to load a 128x128 Map, I set MAP_X and MAP_Z to 128.
I havent tried your canges yet, because this is the Pc in my School :-). So, I will try it later.

I guess i got a error Message, but i dont remember its text....
I will post it in a few hours....

Cu

_____
void signatur();
Striker47
Striker47
Ok, u have uploaded the source code, so this post wont be 3 meters long :-).
www.halof.com/download/terrain_src.zip


If you can load a 256x256 Heighmap, can you say me how? I only get an error windows error message, that some adresses in memory couldn''t be read/written.
If i try to start debuggin it, it crashes in the line with
terrain[x][z][1] = (float)imageData[(z*MAP_Z+x)*3]; 
.


How to fix it...?

(yes, i know... bad english *gG*)

Thx4all,
Striker

____________________
void signatur();

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.