Jump to content
  • Advertisement
Sign in to follow this  
tnutty

HeightMaps

This topic is 3494 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 am trying to create a terrain through height mapping. What I am doing is creating a 2d array, 32 x 32. Then creating a 32 x 32 texture for the height map. Then creating a char buf[32][32], to import raw data from of size 32x32. This is the code for importing data :
void readPic(char * filename)
{
	FILE* pFile = NULL;

	pFile = fopen(filename,"rb");
	if(!pFile)
		cerr << "error reading file";

	fread(&buf,sizeof(buf),1,pFile);
		
	fclose(pFile);
}

And in my initGL function
	for(int i = 0; i < landSize; i++) //land size is declared as a power of 2
		for(int j =0; j< landSize; j++)
		{			
			Land[j][0] = i;
			Land[j][1] = (float)(buf[j])/7.0;//scaling  
			Land[j][2] = j;
		}

I am not sure why its producing a linear sine wave with high frequency. It should produce a small lump in the middle of the grid since the heightmap is created that way. If nothing is wrong there, then can it be my glBegin(GL_QUADS)?

Share this post


Link to post
Share on other sites
Advertisement
What does your drawing code look like? The initGL function just sets up the data, it does not actually draw it.
Also, you don't need to store the x and z coords in your array, only the y (height) coord. Then when you draw, you draw each quad at i, height, j:

// init
for(int i = 0; i < landSize; i++) { //land size is declared as a power of 2
for(int j =0; j< landSize; j++) {
Land[j] = (float)(buf[j])/7.0;//scaling
}
}

// draw
for(int i = 0; i < landSize; i++) { //land size is declared as a power of 2
for(int j =0; j< landSize; j++) {
//draw quad at (i, Land[j], j)
}
}


Unless of course you intend to change the mesh so it's not just a grid with height, but that is AFAIK how height maps are done.

Share this post


Link to post
Share on other sites
This is my drawing code :


glBegin(GL_QUADS);
for( int i =0;i<landSize-2;i++)
{

for( int j=0;j<landSize-2;j++)
{
tx = (float)i/(float)(landSize);
ty = (float)j/(float)(landSize);
tx1 = (float)(i+1)/(float)(landSize);
ty1 = (float)(j+1)/(float)(landSize);

//glColor3f(colLand[j][0],colLand[j][1],colLand[j][2]);
glTexCoord2f(tx,ty); glVertex3f(Land[j][0],Land[j][1],Land[j][2]);
//glColor3f(colLand[i+1][j][0],colLand[i+1][j][1],colLand[i+1][j][2]);
glTexCoord2f(tx1,ty); glVertex3f(Land[i+1][j][0],Land[i+1][j][1],Land[i+1][j][2]);
//glColor3f(colLand[i+1][j+1][0],colLand[i+1][j+1][1],colLand[i+1][j+1][2]);
glTexCoord2f(tx1,ty1); glVertex3f(Land[i+1][j+1][0],Land[i+1][j+1][1],Land[i+1][j+1][2]);
//glColor3f(colLand[j+1][0],colLand[j+1][1],colLand[j+1][2]);
glTexCoord2f(tx,ty1); glVertex3f(Land[j+1][0],Land[j+1][1],Land[j+1][2]);

}

}
glEnd();




Here is a pic of it : http://img195.imageshack.us/img195/9823/land.jpg

Share this post


Link to post
Share on other sites
Quote:
Original post by tnutty
Anyone got any answers?
From the picture, you seem to have i and j reversed when you iterate over the heightmap.

Share this post


Link to post
Share on other sites
Quote:
Original post by tnutty
Then creating a char buf[32][32], to import raw data from of size 32x32.

This is the code for importing data :
*** Source Snippet Removed ***


I doubt your read code will function correctly.. try reading in the values and then writing them out again to see if you get identical files, if you want to check it. I'm not sure how 2d-arrays will be laid out in memory, or if it's even guaranteed to be a certain way, but I would try reading into a 1d-array instead and then converting it.

Share this post


Link to post
Share on other sites
Well it is in BYTE. I change the size to 256x256 and its better but still bad.
The lumps looks likes weired though. Take a look :
http://img190.imageshack.us/img190/38/88573827.jpg

Share this post


Link to post
Share on other sites
If you are getting incorrect values in your 2D array, it is a read problem.
Have you tried reading the BYTEs in one at a time and fill the 2D array that way. I think what you are finding is what Erik Rufelt said - 2D arrays are not just a chunk of contiguous memory.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!