# Simple Heightmap generation problem

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

## Recommended Posts

Hello. I just started to learn terrain rendering and generation. I am now able to render a map given a heightmap properly with the normals etc (well its very basic but it works so im happy :) ). So i started trying to generate a heightmap. I took my simple algorithm from here: http://www.lighthouse3d.com/opengl/terrain/index.php?fault My problem is, the same heightmap is ALWAYS generated. But random seems to work well everywhere else so i cant find the problem. Also the height seems to only increase and never decrease like it should (heightMap[x][y] -= 0.01;) The heightmap is properly initialized to 0 before the height generation. heres my code:
	/**generate a random heightmap*/
float v, a, b, c, d;
for( int i = 0; i < 1000; ++i )
{
v = rand();
a = sin(v);
b = cos(v);
d = hypotf(size_x,size_y);
c = ( rand() / RAND_MAX ) * d - d/2;

for( int x = 0; x < (int)size_x; ++x )
{
for( int y = 0; y < (int)size_y; ++y )
{
if ( a*x + b*y - c > 0 )
heightMap[x][y] += 0.01;
else
heightMap[x][y] -= 0.01;
}
}
}


##### Share on other sites
ugh nevermind i found the problem...
i was doing rand() / RAND_MAX but since rand() is an int it gave always the same result ( 1 ).
I just did (float)rand()/RAND_MAX -> problem solved.
Thanks anyways.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633679
• Total Posts
3013298
×