Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 24th, 2007, 1:36 PM   #1
csrocker101
Programmer
 
Join Date: Dec 2006
Posts: 50
Rep Power: 2 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
Old Apr 24th, 2007, 3:32 PM   #2
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 244
Rep Power: 3 Seif is on a distinguished road
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.
Seif is offline   Reply With Quote
Old Apr 24th, 2007, 5:34 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 24th, 2007, 6:50 PM   #4
csrocker101
Programmer
 
Join Date: Dec 2006
Posts: 50
Rep Power: 2 csrocker101 is on a distinguished road
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.
csrocker101 is offline   Reply With Quote
Old Apr 24th, 2007, 7:15 PM   #5
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 244
Rep Power: 3 Seif is on a distinguished road
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 and I dare say if you are getting problems, you maybe confused with whats goin on yourself.
Seif is offline   Reply With Quote
Old Apr 24th, 2007, 9:02 PM   #6
csrocker101
Programmer
 
Join Date: Dec 2006
Posts: 50
Rep Power: 2 csrocker101 is on a distinguished road
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.
csrocker101 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
random numbers on form load ?_? cloud- Visual Basic 6 Oct 25th, 2008 4:44 PM
Random Numbers crawforddavid2006 C++ 24 Jun 19th, 2006 11:27 PM
Median/Mode in arrays? {Need help} Java|Tera Java 27 Nov 29th, 2005 11:50 AM
random numbers in 2D array cwl157 Java 4 Apr 29th, 2005 7:08 AM
Random Numbers Dark Flare Knight Java 7 Apr 8th, 2005 1:23 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:23 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC