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

Started by
8 comments, last by ajm113 16 years, 2 months ago
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);
	}

Check out my open source code projects/libraries! My Homepage You may learn something.
Advertisement
what happens if you remove "srand(10);"?

You should only really need to call srand once at the start of your program, and give it some "randomish" value, like the current time.
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.
Check out my open source code projects/libraries! My Homepage You may learn something.
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);}//  orfor (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!
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	}
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.
Check out my open source code projects/libraries! My Homepage You may learn something.
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.
Thanks works perfectly jouley!
Check out my open source code projects/libraries! My Homepage You may learn something.
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.
Oh, yes and thanks Hodgman! I appreciate your guy's help!
Check out my open source code projects/libraries! My Homepage You may learn something.

This topic is closed to new replies.

Advertisement