View Single Post
Old Apr 24th, 2007, 12:36 PM   #1
csrocker101
Programmer
 
Join Date: Dec 2006
Posts: 47
Rep Power: 0 csrocker101 is on a distinguished road
random Numbers in openGL

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.
csrocker101 is offline   Reply With Quote