Public Group

HeightMaps

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

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)

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 on other sites
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:
// initfor(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      }}// drawfor(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 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 on other sites
Quote:
 Original post by tnuttyAnyone got any answers?
From the picture, you seem to have i and j reversed when you iterate over the heightmap.

Share on other sites
Quote:
 Original post by tnuttyThen 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 on other sites
When using 1d or 2d I get different values,as well as some negatives.

Share on other sites
Perhaps you should read it as unsigned char, not char.

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 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.

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 10
• 10
• 14
• 10
• 25
• Forum Statistics

• Total Topics
632647
• Total Posts
3007640
• Who's Online (See full list)

There are no registered users currently online

×