HeightMaps

Started by
19 comments, last by tnutty 14 years, 11 months ago
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)?
Our whole life is a opengl application.
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:
// 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.
[Window Detective] - Windows UI spy utility for programmers
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
Our whole life is a opengl application.
Anyone got any answers?
Our whole life is a opengl application.
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.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

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.
When using 1d or 2d I get different values,as well as some negatives.


Our whole life is a opengl application.
Perhaps you should read it as unsigned char, not char.
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
Our whole life is a opengl application.
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.

This topic is closed to new replies.

Advertisement