srand() supplies a seed to initialise the random number generator. Normally you would do this just once at the start of the program.time(NULL) is used.int random = rand() % 5;
cout << random << endl;
if (random < 2){
squareColour = 0;
}else{
squareColour = 1;
}
glColor3f(squareColour, squareColour, squareColour); |
| with time(NULL) my colour keeps changing even after the squares have been displayed which i dont want. |
void OGWindow::drawGrid(GLvoid){
int squareColour = 1;
int size = 7;
srand(time(NULL));
for (int xValue = 0; xValue < size; xValue++)
{
for (int yValue = 0; yValue < size; yValue++)
{
if (xValue == 0 && yValue == 0)
{
glColor3f(1, 0, 0);
//code removed, draws square
glColor3f(0, 0, 0);
//start square outline
} else if (xValue == size - 2 && yValue == size - 2){
glColor3f(0, 0, 1);
//an end square
glColor3f(0, 0, 0);
//end square outline
} else {
glColor3f(0, 0, 0);
//square outline
int random = rand() % 5;
//int random = 1 + rand( ) % 5;
cout << random << endl;
if (random < 2){
squareColour = 0;
}else{
squareColour = 1;
}
glColor3f(squareColour, squareColour, squareColour);
//making a square
glBegin(GL_QUADS);
glVertex3f(xValue-0.5, yValue-0.5, 0.0);
glVertex3f(xValue+0.5, yValue-0.5, 0.0);
glVertex3f(xValue+0.5, yValue+0.5, 0.0);
glVertex3f(xValue-0.5, yValue+0.5, 0.0);
glEnd();
}
}
}
} |
srand() call should not be here inside this function at all, it should be in the main function.squareColour = savedColour[xValue][yValue]; instead of using the random number each time.rand() in that function, then save the value to the array. |
|
srand() then initColour()saveColour[xValue][yValue] inside function OGWindow::drawGrid(GLvoid) in place of the existing random number stuff.
void initColour()
{
int squareColour = 1;
for (int xValue = 0; xValue < size; xValue++)
{
for (int yValue = 0; yValue < size; yValue++)
{
if (rand() % 5 < 2)
squareColour = 0;
else
squareColour = 1;
saveColour[xValue][yValue] = squareColour;
}
}
}
void OGWindow::drawGrid(GLvoid){
int size = 7;
//srand();
for (int xValue = 0; xValue < size; xValue++)
{
for (int yValue = 0; yValue < size; yValue++)
{
if (xValue == 0 && yValue == 0)
{
glColor3f(1, 0, 0);
//start square
glBegin(GL_QUADS);
glVertex3f(xValue-0.3, yValue-0.3, 0.0);
glVertex3f(xValue+0.3, yValue-0.3, 0.0);
glVertex3f(xValue+0.3, yValue+0.3, 0.0);
glVertex3f(xValue-0.3, yValue+0.3, 0.0);
glEnd();
glColor3f(0, 0, 0);
//start square outline
glBegin(GL_LINE_LOOP);
glVertex3f(xValue-0.5, yValue-0.5, 0.0);
glVertex3f(xValue+0.5, yValue-0.5, 0.0);
glVertex3f(xValue+0.5, yValue+0.5, 0.0);
glVertex3f(xValue-0.5, yValue+0.5, 0.0);
glEnd();
} else if (xValue == size - 2 && yValue == size - 2){
glColor3f(0, 0, 1);
//an end square
glBegin(GL_QUADS);
glVertex3f(xValue-0.3, yValue-0.3, 0.0);
glVertex3f(xValue+0.3, yValue-0.3, 0.0);
glVertex3f(xValue+0.3, yValue+0.3, 0.0);
glVertex3f(xValue-0.3, yValue+0.3, 0.0);
glEnd();
glColor3f(0, 0, 0);
//end square outline
glBegin(GL_LINE_LOOP);
glVertex3f(xValue-0.5, yValue-0.5, 0.0);
glVertex3f(xValue+0.5, yValue-0.5, 0.0);
glVertex3f(xValue+0.5, yValue+0.5, 0.0);
glVertex3f(xValue-0.5, yValue+0.5, 0.0);
glEnd();
} else {
glColor3f(0, 0, 0);
//square outline
glBegin(GL_LINE_LOOP);
glVertex3f(xValue-0.5, yValue-0.5, 0.0);
glVertex3f(xValue+0.5, yValue-0.5, 0.0);
glVertex3f(xValue+0.5, yValue+0.5, 0.0);
glVertex3f(xValue-0.5, yValue+0.5, 0.0);
glEnd();
//saveColour[xValue][yValue];
glColor3f(squareColour, squareColour, squareColour);
//making a square
glBegin(GL_QUADS);
glVertex3f(xValue-0.5, yValue-0.5, 0.0);
glVertex3f(xValue+0.5, yValue-0.5, 0.0);
glVertex3f(xValue+0.5, yValue+0.5, 0.0);
glVertex3f(xValue-0.5, yValue+0.5, 0.0);
glEnd();
}
}
}
} |
|
|
|
|
| [code] |
| [/code] |
|
|
srand(); and initColour(); at the start of the program?