![]() |
random Numbers in openGL
:confused: I am having some problems with random numbers (among a list of other things) in my openGL program I am trying to finish. I have a random number generator that generates a random number from 50-400 which is supplied to the y value of a quad. My problem is, when I supply the random number to the quad and then draw it in my openGL draw function it continues to generate random numbers and my quads look like a music equalizer bar as they continually jump up and down in height. I tried setting it in the constructor which works, but everytime I start the program a new height needs to be generated and setting it in the constructor intializes it once and it never changes after that. Here is my building code:
:
#pragma onceAs you can see the random numbers are held in array and then supplied to the y values since I need different heights. I've been told I need to "set" them before I use them but I am not sure how to go about doing this. Here is my drawcode in my main function if needed :
void DrawScene(void)Not exactly sure how I'd go about doing this and making them stay fixed and not look like an equalizer bar, but generate new random numbers everytime the program starts. I would appreciate any incite or help in this matter. |
you are creating new random numbers each time you call the drawBuildings member function, which you are calling every frame.
The reason they are always the same size when called from the constructor is because the seed of the sequence of the random number is always the same. If you want to change the heights of the buildings each time the application starts use something like this before you generate the numbers from within the constructor : :
srand(time(0));this function sets the seed to an integer value returned from time(0);, which gets the current time from the system clock. If you want to change the heights at some point in the future at runtime, create another member function such as which does as suggested in the constructor. 2 things I would recommend you doing : 1) look into how pseudo random numbers work in C++. A google search will pull up loads, heres one i found quick for you. http://www.fredosaurus.com/notes-cpp/misc/random.html 2) Take a long look at your code and understand what its doing and why. |
I might add that a reseeding at very frequent intervals might result in a reseeding with the same value, which will result in a sequence of "random" numbers that are identical to the the previous sequence. You might want to investigate truly random generation, or perturb the seed with something less predictable.
|
Thanks seif. Ok basically I rechanged my code and heres how it looks now probably really inefficient but oh well..
:
Buildings() //constructor intializes variablesIt works perfectly fine when I dont put :
srand(time(0));When I put :
srand(time(0)); |
well firstly i thought if you are drawing a quad for each building, glEnd() should be placed after the four vertices ideally, or after the foor loop.
One thing I dont get is :
//draw 6 buildings :Dwhere in the drawBuilding member function, you are drawing 6 buildings. If you have instantiated 6 buildings, and for each of them buildings class you are drawing another 6 buildings, you now got 36 buildings. Is this intentional ? Your naming of objects is very confusing to me :confused: and I dare say if you are getting problems, you maybe confused with whats goin on yourself. |
ya as you can tell I'm a pretty crap programmer. I really have no idea what I'm doing. :o It wasn't my intention to draw 36 quad. I just wasn't sure how to use the array values I set in the constructor in the drawBuildings(). Since the "i" went out of scope as I went down to the other method.
|
| All times are GMT -5. The time now is 2:07 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC