Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   random Numbers in openGL (http://www.programmingforums.org/showthread.php?t=13051)

csrocker101 Apr 24th, 2007 1:36 PM

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 once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <GL/glut.h>
#include <atlstr.h>
#include <gl\gl.h>
#include <gl\glu.h>

class Buildings
{
   
        private:
          double random_Height;
          float red, green, blue;
        public:
                double x, y;
                double randomHeightArray[5];
       
               
                Buildings() //constructor intializes variables
                {
                        x = 0.0;
                        y = 0.0;
                        red = 0.0;
                        green = 0.0;
                        blue = 1.0;
                }
                void drawBuildings() //method which draws quads for buildings
                {
                        for(int i = 0; i < 5; i ++) //for loop needed for 6 different random heights
                        {
                                randomNumberArray[i] = (rand() % 400) + 50; // assigns 6 diff heights to array
                               
                                glBegin(GL_QUADS); //draws quad
                                        glColor3f(red, green, blue);
                                        glVertex2d(x + 0, y + 0);
                                        glVertex2d(x + 0, y + randomNumberArray[i]); //uses random height
                                        glVertex2d(x + 100, y + randomNumberArray[i]); //uses random height
                                        glVertex2d(x + 100, y + 0);
                        }       
                }
};


As 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)
{

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        //draw 6 buildings :D

        for(int i = 0; i < 6; i++)
        {
                b[i].drawBuildings();
        }
        for(int i = 0; i < 5; i++)
        {
                b[i].x = b[i -1].x + 100;
        }
               
        glEnd();

        // End drawing code


        glutSwapBuffers();
}


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.

Seif Apr 24th, 2007 3:32 PM

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.

DaWei Apr 24th, 2007 5:34 PM

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.

csrocker101 Apr 24th, 2007 6:50 PM

Thanks seif. Ok basically I rechanged my code and heres how it looks now probably really inefficient but oh well..
:

Buildings() //constructor intializes variables
                {
                        x = 0.0;
                        y = 0.0;
                        red = 0.0;
                        green = 0.0;
                        blue = 1.0;
                        srand(time(0));
                        for(int i = 0; i < 6; i ++) //for loop needed for 6 different random heights
                        {
                                randomHeightArray[i] = rand() % 400 + 50; // assigns 6 diff heights to array
                        }
                }
                void drawBuildings() //method which draws quads for buildings
                {
                        for(int i = 0; i < 6; i++)
                        {
                                glBegin(GL_QUADS); //draws quad
                                                glColor3f(red, green, blue);
                                                glVertex2d(x + 0, y + 0);
                                                glVertex2d(x + 0, y + randomHeightArray[i]); //uses random height
                                                glVertex2d(x + 100, y + randomHeightArray[i]); //uses random height
                                                glVertex2d(x + 100, y + 0);
                        }
                       
                }


It works perfectly fine when I dont put
:

srand(time(0));
into the code. When I don't put it in there, it intializes all the values to a random height but of course is a one time set only and the heights stay the same.

When I put
:

srand(time(0));
back into it, for some reason it only takes the first value of the array. I am not sure how to fix it but very close! It does however generate random numbers. I checked with a breakpoint and each index of the array has a random value, but it only takes the first index of the array. Any suggestions on how to fix this? Could you also if you have time comment on any aspects of my design? Whats good and how to make it more efficient. Cheers.

Seif Apr 24th, 2007 7:15 PM

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 :D

        for(int i = 0; i < 6; i++)
        {
                b[i].drawBuildings();
        }


where 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.

csrocker101 Apr 24th, 2007 9:02 PM

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