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.
🔒 Locked
Heightmap problem
Advertisement
Original Post
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 - 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]
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.
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.
My Height() function was "borrowed" from DigiBen''s tutorial on heightmapping (gametutorials.com)
it looks like this:
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
}
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.
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.
Advertisement