Skip to main content
gamedev.net
🔒 Locked

Heightmap prob

Started by shalrath Jul 1, 2004 at 11:33 PM 1 replies 500+ views
Original Post
shalrath
shalrath
I've already posted this problem over in the 'General Programming' forum, but it seems to have ground to a halt. Here's the URL to the original post: http://www.gamedev.net/community/forums/topic.asp?topic_id=254186 And here's the code as it is at the moment:

//From DigiBens Heightmapping tut #2 (www.gametutorials.com)
byte_t hmap::Height(byte_t *pHeightMap, int X, int Y)
{
	int x = X % map_2Dheight;					// Error check our x value
	int y = Y % map_2Dwidth;					// Error check our y value

	if(!pHeightMap) return 0;				// Make sure our data is valid

    return pHeightMap[x + (y * map_2Dwidth)];	// Index into our height array and return the height
}

bool hmap::CalcHeightMap(void)
{
	int x = 0;
	int y = 0;
	bool goback = false; //From DigiBens Heightmapping tut #2 (www.gametutorials.com)

	fnumverts = map_2Dwidth*map_2Dheight;

	hmapdata = new float[fnumverts*3];
	texcoords = new float[fnumverts*2]; 

	for (x = 0; x < map_2Dheight - 1; x++)
	{
		if (goback)
		{
			for (y = map_2Dwidth - 1; y >= 0; y--)
			{
				//--------------------------------------------------------------
				hmapdata[x + (y*map_2Dwidth)] = x;
				hmapdata[(x + (y*map_2Dwidth)) + 1] = Height(data,x,y);
				hmapdata[(x + (y*map_2Dwidth)) + 2] = y;
				
				texcoords[x + (y*map_2Dwidth)] = (float)(x/map_2Dheight);
				texcoords[(x + (y*map_2Dwidth)) + 1] = (float)(-y/map_2Dwidth);
				//--------------------------------------------------------------

				//--------------------------------------------------------------
				hmapdata[(x+1) + (y*map_2Dwidth)] = x + 1;
				hmapdata[((x+1) + (y*map_2Dwidth)) + 1] = Height(data,x+1,y);
				hmapdata[((x+1) + (y*map_2Dwidth)) + 2] = y;

				texcoords[(x+1) + (y*map_2Dwidth)] = (float)((x+1)/map_2Dheight);
				texcoords[((x+1) + (y*map_2Dwidth)) + 1] = (float)(-y/map_2Dwidth);
				//--------------------------------------------------------------
			}
		}
		else
		{
			for (y = 0; y < map_2Dwidth - 1; y++)
			{
				//--------------------------------------------------------------
				hmapdata[(x+1) + (y*map_2Dwidth)] = x + 1;
				hmapdata[((x+1) + (y*map_2Dwidth)) + 1] = Height(data,x+1,y);
				hmapdata[((x+1) + (y*map_2Dwidth)) + 2] = y;
				
				texcoords[(x+1) + (y*map_2Dwidth)] = (float)((x+1)/map_2Dheight);
				texcoords[((x+1) + (y*map_2Dwidth)) + 1] = (float)(-y/map_2Dwidth);
				//--------------------------------------------------------------

				//--------------------------------------------------------------
				hmapdata[x + (y*map_2Dwidth)] = x;
				hmapdata[(x + (y*map_2Dwidth)) + 1] = Height(data,x,y);
				hmapdata[(x + (y*map_2Dwidth)) + 2] = y;
				
				texcoords[x + (y*map_2Dwidth)] = (float)(x/map_2Dheight);
				texcoords[(x + (y*map_2Dwidth)) + 1] = (float)(-y/map_2Dwidth);
				//--------------------------------------------------------------
			}
		}
		goback = !goback;
	}
lonesock
lonesock
1st, I assume that data is a global of type byte_t?

2nd, shouldn't all of your indecies look like:
(note the "* 3" and "* 2")

hmapdata[(x + (y*map_2Dwidth)) * 3] = x;
texcoords[(x + (y*map_2Dwidth)) * 2] = (float)(x/map_2Dheight);

still, that should make your code safer (since you're only reading the first half or third of the array). There are no max array sizes, other than system memory limitations on float arrays. If you go above maxint however, make sure your size (the "3*width*height" part) is of type long.
Hamster
Hamster
Lone sock is right. All instances of this sort of thing
hmapdata[x + (y*map_2Dwidth)] = some_xval;hmapdata[x + (y*map_2Dwidth)+1] = some_yval;hmapdata[x + (y*map_2Dwidth)+2] = some_zval;

need to be replaced with something more like this:
hmapdata[ (x + (y*map_2Dwidth)) * 3 + 0] = some_xval;hmapdata[ (x + (y*map_2Dwidth)) * 3 + 1] = some_yval;hmapdata[ (x + (y*map_2Dwidth)) * 3 + 2] = some_zval;


in your current code you want to group vertex coordinates in threes, but are only shifting one along the array before assigning your next group of floats. This means that two of your values will overwrite two of those that had already been written. By multiplying the offset by three and then adding 0 to get to x, 1 to get to y and 2 to get to z, your values shouldn't pile on top of one another.

The same technique would need to be used for your texture coordinates, although assuming you are using two dimensional textures you'd actually need to multiply by two.

Here is a quick snippet from a function of mine that creates a terrain object having been passed a heightmap.

  unsigned int i, j;  m_pfVertexBuffer = new float[ tHeightMap.m_uiSize * tHeightMap.m_uiSize * 3 ];  m_pfTexCoordBuffer = new float[ tHeightMap.m_uiSize * tHeightMap.m_uiSize * 2 ];  for( i = 0; i < tHeightMap.m_uiSize; i++ )  {    for( j = 0; j < tHeightMap.m_uiSize; j++ )    {      m_pfVertexBuffer[ ( i + j * tHeightMap.m_uiSize ) * 3 ] = (float)i;      m_pfVertexBuffer[ ( i + j * tHeightMap.m_uiSize ) * 3  + 1 ] = tHeightMap.m_pfHeights[( i + j * tHeightMap.m_uiSize )];      m_pfVertexBuffer[ ( i + j * tHeightMap.m_uiSize ) * 3  + 2 ] = (float)j;      m_pfTexCoordBuffer[ ( i + j * tHeightMap.m_uiSize ) * 2 ] = (float)i/(float)(tHeightMap.m_uiSize-1);      m_pfTexCoordBuffer[ ( i + j * tHeightMap.m_uiSize ) * 2 + 1] = 1.0f-(float)j/(float)(tHeightMap.m_uiSize-1);    }  }

Topic Locked

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

Sign in to reply to this topic.