Skip to main content
gamedev.net
🔒 Locked

Heightmap problem

Started by shalrath Dec 26, 2003 at 12:53 AM 10 replies 600+ views
Original Post
shalrath
shalrath
I''m using the following code to cycle through a RAW file (greyscale, 128x64), the integer ''x'' stores the current "vertical" value as we loop from 0 to map_2Dheight(64) down the image, and ''y'' holds the "horizontal" value as we go through from 0 to map_2Dwidth(128). After getting approximately 117-121 loops through the first collumn (x=0, y=117-121), this program gives me the "memory cannot be read" windows error. Just wondering if anyone can see what might be going wrong.
	for (x = 0; x < map_2Dheight; x++)
	{
		if (goback)
		{
			for (y = map_2Dwidth; y > 0; y--)
			{
				glColor4f(0.0f,1.0f,0.0f,y/100);
				
				glVertex3f(x, Height(data,x,y), y);
				SetTexCoord(x,y);

				glVertex3f((x+1), Height(data,x+1,y), y);
				SetTexCoord((x+1),y);
			}
		}
		else
		{
			for (y = 0; y < map_2Dwidth; y++)
			{

				glColor4f(1.0f,0.0f,0.0f,y/100);

				glVertex3f((x+1), Height(data,x+1,y), y);
				SetTexCoord(x+1,y);

				glVertex3f(x, Height(data,x,y), y);
				SetTexCoord(x,y);
			}
		}
		goback = !goback;
	}
Cheers.
shalrath
shalrath
Anyone?
Trienco
Trienco
look at your indices and ask yourself: does an array of size width/height really have indices from 1 to width and from 0 to height?
f@dzhttp://festini.device-zero.de
Thorin_nz
Thorin_nz
Try this...

for (x = 0; x < map_2Dheight; x++)
{
if (goback)
{
for (y = map_2Dwidth - 1; y >= 0; y--)
...
shalrath
shalrath
made that alteration, and I''m still getting a memory error after the first 117-121 iterations... (which are made by the second ''y'' loop). Do I need to allocate any other memory before doing any of this?
Kaezin
Kaezin

for (x = 0; x < map_2Dheight - 1; x++)
{
if (goback)
{
for (y = map_2Dwidth - 1; y > 0; y--)
{
glColor4f(0.0f,1.0f,0.0f,y/100);

glVertex3f(x, Height(data,x,y), y);
SetTexCoord(x,y);

glVertex3f((x+1), Height(data,x+1,y), y);
SetTexCoord((x+1),y);
}
}
else
{
for (y = 0; y < map_2Dwidth - 1; y++)
{

glColor4f(1.0f,0.0f,0.0f,y/100);

glVertex3f((x+1), Height(data,x+1,y), y);
SetTexCoord(x+1,y);

glVertex3f(x, Height(data,x,y), y);
SetTexCoord(x,y);
}
}
goback = !goback;
}


Try that. You're probably reading past the end of the array.

I simply changed it from going up to map_2Dwidth to map_2Dwidth - 1, same with map_2Dheight.

[edited by - Kaezin on January 2, 2004 1:21:46 AM]
Half the people you know are below average.Trogdor the Burninator
onebeer
onebeer
Kaezin has it right, you are going outside of your bitmap.

The problem with height maps is the dimensions of the map. It has to be one bigger than you think. For example, if I used a 1x1 bitmap for the height map, I would still have two points to build the terrain. The code would not be able to do:
for (x = 0; x < bitmap.width; x++)
because you have to do code like you do:
glVertex3f((x + 1), Height(data, x + 1,y), y);
The "Height(data, x + 1, y)" adds +1 to x, which goes out of bounds of the 1x1 bitmap.
You either have to do a bitmap that is 1 dimension bigger than the desired height map or duplicate the last values to give the last vertex something to connect to.

onebeer
shalrath
shalrath
After changing the code as Kaezin suggested, it now crashes 125 iterations through the first ''y'' loop. (where x=1 and y=125) I''ve tried different images for my heightmap, and this had no effect. Any ideas?
Kaezin
Kaezin
I posted the code with no idea what your Height() function looks like and I posted it just in case you were reading outside your array. I never meant it to be the absolute solution...
Half the people you know are below average.Trogdor the Burninator
shalrath
shalrath
My Height() function was "borrowed" from DigiBen''s tutorial on heightmapping (gametutorials.com)

it looks like this:


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

}
Mr Grinch
Mr Grinch
I haven''t tried this out or anything, but I would guess that you have your width and height mixed up. I think y should go with height and x with width. If you had a square texture that would probably still run ok, but since you have a non-square texture, I believe that is causing your problems. I suspect that you give height 128 and width 64, when it may need to be the other way around.
shalrath
shalrath
I had tried this, but it''s cycling through the map the wrong way, and I get the image divided into 2 sections put next to each other for instance, if I put the letter ''H'' on my heightmap texture, I end with a heightmap that looks like ''I U I'' as though the end of the heightmap has been cut off and stuck on the sides.

Topic Locked

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

Sign in to reply to this topic.