Sign in to follow this  

Simple Heightmap generation problem

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this