csharp100

10-12-2013, 04:30 PM

Hi everyone, I am almost finished with my project but my rand() function doesn't seem to be working. I have two global variables set as:

const GLint CUBE_HIGH = 15;

const GLint CUBE_LOW = 7;

and the number of cubes I am getting is only 7. I cannot seem to figure out why so I am hoping someone can point out my mistake and show me what I need to do to fix it. Here is what I believe is the relevant code:

The function call to create the amount of cubes and the cubes random x & y axis which is called in main:

void initOrigins()

{

maxCubes = rand() / RAND_MAX * (CUBE_HIGH - CUBE_LOW) + CUBE_LOW;

for (int i = 0; i < maxCubes; i++)

{

cubeOrigins[i].x = X_LOW + (float)rand() / (float)RAND_MAX * (X_HIGH - X_LOW);

cubeOrigins[i].y = Y_LOW + (float)rand() / (float)RAND_MAX * (Y_HIGH - Y_LOW);

}

}

and this is my display function:

void myDisplay()

{

//maxCubes = CUBE_LOW + rand() / RAND_MAX * (CUBE_HIGH - CUBE_LOW);

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

glClear(GL_COLOR_BUFFER_BIT);

for(int i = 0; i < maxCubes; i++)

{

glLoadIdentity();

glTranslatef(cubeOrigins[i].x, cubeOrigins[i].y, -120.0);

glTranslatef(cubeOrigins[i].x, cubeOrigins[i].y, cubeZ);

glRotatef(rotateY, 0.0f, 1.0f, 0.0f);

glutWireCube(2.0f);

}

cubeZ += 0.050f;

glutSwapBuffers();

glFlush();

if (cubeZ > 120.0f)

{

cubeZ -= 120.f;

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

glClear(GL_COLOR_BUFFER_BIT);

for(int i = 0; i < maxCubes; i++)

{

glLoadIdentity();

glTranslatef(cubeOrigins[i].x, cubeOrigins[i].y, -120.0);

glTranslatef(cubeOrigins[i].x, cubeOrigins[i].y, cubeZ);

glRotatef(rotateY, 0.0f, 1.0f, 0.0f);

glutWireCube(2.0f);

}

cubeZ += 0.050f;

glutSwapBuffers();

glFlush();

}

}

Thank you everyone.

const GLint CUBE_HIGH = 15;

const GLint CUBE_LOW = 7;

and the number of cubes I am getting is only 7. I cannot seem to figure out why so I am hoping someone can point out my mistake and show me what I need to do to fix it. Here is what I believe is the relevant code:

The function call to create the amount of cubes and the cubes random x & y axis which is called in main:

void initOrigins()

{

maxCubes = rand() / RAND_MAX * (CUBE_HIGH - CUBE_LOW) + CUBE_LOW;

for (int i = 0; i < maxCubes; i++)

{

cubeOrigins[i].x = X_LOW + (float)rand() / (float)RAND_MAX * (X_HIGH - X_LOW);

cubeOrigins[i].y = Y_LOW + (float)rand() / (float)RAND_MAX * (Y_HIGH - Y_LOW);

}

}

and this is my display function:

void myDisplay()

{

//maxCubes = CUBE_LOW + rand() / RAND_MAX * (CUBE_HIGH - CUBE_LOW);

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

glClear(GL_COLOR_BUFFER_BIT);

for(int i = 0; i < maxCubes; i++)

{

glLoadIdentity();

glTranslatef(cubeOrigins[i].x, cubeOrigins[i].y, -120.0);

glTranslatef(cubeOrigins[i].x, cubeOrigins[i].y, cubeZ);

glRotatef(rotateY, 0.0f, 1.0f, 0.0f);

glutWireCube(2.0f);

}

cubeZ += 0.050f;

glutSwapBuffers();

glFlush();

if (cubeZ > 120.0f)

{

cubeZ -= 120.f;

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

glClear(GL_COLOR_BUFFER_BIT);

for(int i = 0; i < maxCubes; i++)

{

glLoadIdentity();

glTranslatef(cubeOrigins[i].x, cubeOrigins[i].y, -120.0);

glTranslatef(cubeOrigins[i].x, cubeOrigins[i].y, cubeZ);

glRotatef(rotateY, 0.0f, 1.0f, 0.0f);

glutWireCube(2.0f);

}

cubeZ += 0.050f;

glutSwapBuffers();

glFlush();

}

}

Thank you everyone.