Sign in to follow this  

Tree's are in a line? (C++/OpenGL)

This topic is 3594 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

For some reason I get my trees rendered in OpenGL spawn in random places, but they seem to be in a line, but in random place around together. This is what it looks: Image and video hosting by TinyPic Doesn't look "random" that much doesn't it?
	//Render Trees

	srand(10);

  for (int n = 0; n < 10; n++)
  {

		int TreeType = (rand()%4)+1;
		int mp_x = (rand()%10)+1;
		int mp_z = (rand()%10)+1;

		glTranslatef(mp_x,0.0f,mp_z);
		DrawTree(TreeType, 4.0);
	}

Share this post


Link to post
Share on other sites
Tree's will start jumping around the area so it would look wired. I need to keep the current values of the random number so that's why I need it.

I am already giving it a random values for the positions. I'm just do not under stand why it's in a specific area that it does it in a row look.

Share this post


Link to post
Share on other sites
Ditto to what Hodgman said, but just another observation. For each new tree, you're just adding a position to the previous tree's position. So, you'll never find tree i+1 getting placed on the other side of tree i-1 than tree i. Your values for mp_x and mp_z are always positive. The way OpenGL (and graphics in general) treats transformations is as a series of matrix multiplications. When you translate once, you "move" to a spot. Another translation will, unless you tell it otherwise, start "moving" from that spot. Effectively, after enough trees, you'll be placing them at infinity. Or INT_MAX, anyway.

In short, you have a couple options to not have this happen:
for (int n = 0; n < 10; n++)
{
int TreeType = (rand()%4)+1;
int mp_x = (rand()%10)+1;
int mp_z = (rand()%10)+1;

glTranslatef(mp_x,0.0f,mp_z);
DrawTree(TreeType, 4.0);

// undo the previous translation
glTranslatef(-mp_x, 0.0f, -mp_z);
}

// or

for (int n = 0; n < 10; n++)
{
int TreeType = (rand()%4)+1;
int mp_x = (rand()%10)+1;
int mp_z = (rand()%10)+1;

// start with a new copy of the current transformation matrix
glPushMatrix( );

// transform this new matrix
glTranslatef(mp_x,0.0f,mp_z);
DrawTree(TreeType, 4.0);

// don't forget to pop that new matrix off of the matrix stack
glPopMatrix( );
}

Your problems are probably a combination of the srand(10) issue and this. If not, let us know!

Share this post


Link to post
Share on other sites
Quote:
Original post by ajm113
Tree's will start jumping around the area so it would look wired. I need to keep the current values of the random number so that's why I need it.

Ahh, I see - the random numbers are generated and used in your render loop.

Instead of generating the same 'random' numbers every frame, why not generate them once and store the values?

struct TreeData
{
TreeData( int t, int x, int z ) : TreeType(t), mp_x(x), mp_z(z) {}
int TreeType, mp_x, mp_z;
};
typedef std::vector<TreeData> TreeVector;
...
TreeVector trees;
//Create trees - do this once at start-up
for(int n = 0; n < 10; n++)
{
trees.push_back( TreeData(rand()%4)+1, (rand()%10)+1, (rand()%10)+1) );
}
...
//Draw trees - do this every frame
for(TreeVector::const_iterator i=trees.begin(); i!=trees.end(); ++i)
{
glPushMatrix( );//important - see jouley's post
glTranslatef(i->mp_x,0.0f,i->mp_z);
DrawTree(i->TreeType, 4.0);
glPopMatrix( );//important - see jouley's post
}




Share this post


Link to post
Share on other sites
mmm, it seems now that the trees do go in random places, but they now stay in 1 and 4rth quarter of the area. Is their a way to fix this?

EDIT: We must have posted at the same time, give me a while to test your code. I have some things too attend now.

Share this post


Link to post
Share on other sites
Quote:
Original post by ajm113
mmm, it seems now that the trees do go in random places, but they now stay in 1 and 4rth quarter of the area. Is their a way to fix this?

You probably also want your random values to be able to be negative. Sticking to ints:
int getRandomInRange(int min, int max)
{
return min + (rand( ) / static_cast<float>(RAND_MAX)) * (max - min);
}
You can (should, if you want more than a few trees in an "organic" arrangement) switch to float pretty easily.

Share this post


Link to post
Share on other sites
Quote:
Original post by ajm113
Thanks works perfectly jouley!

Welcome. And don't forget Hodgman, he's got a good point -- there's no reason to rely on srand([constant]) for the drawing to not be random. Storing the tree data tree by tree will make it much easier to move to a system where you can specify where individual trees should be, in order to be more of a guiding force in your game's environmental design.

Share this post


Link to post
Share on other sites

This topic is 3594 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