# How would I get my cubes to start and fly from different points in the view?

Hi everyone. Love this board! I have almost finished my program except this last thing. I am trying to get my cubes to start in the z plane at different points and fly off the screen. It is like the old “flying windows” screen saver. Right now they all start at a point and move out which is great but they do it uniformly. First some code. This is my display function:

``````
void myDisplay()
{

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
for(int i = 0; i < maxCubes; i++)
{

glTranslatef(0.0f, 0.0f, -120.0);
glTranslatef(cubeOrigins[i].x, cubeOrigins[i].y, cubeZ);
glRotatef(rotateAxis, cubeOrigins[i].rotateX, cubeOrigins[i].rotateY, 0.0f);

glutWireCube(cubeOrigins[i].size);
}

cubeZ += 0.050f;
glutSwapBuffers();

if (cubeZ > 120.0f)
{
cubeZ -= 100.f;
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
for(int i = 0; i < maxCubes; i++)
{
initRandoms();

glTranslatef(0.0f, 0.0f, -120.0);
glTranslatef(cubeOrigins[i].x, cubeOrigins[i].y, cubeZ);
glRotatef(rotateAxis, cubeOrigins[i].rotateX, cubeOrigins[i].rotateY, 0.0f);

glutWireCube(cubeOrigins[i].size);

}
cubeZ += 0.050f;
glutSwapBuffers();
}
}

``````

And this is my random function which is taking care of all the randomness:

``````
void initRandoms()
{
maxCubes = rand() % (CUBE_HIGH - CUBE_LOW + 1) + 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);
//cubeOrigins[i].z = Z_LOW + (float)rand() / (float)RAND_MAX * (Z_HIGH - Z_LOW);
cubeOrigins[i].size = SIZE_LOW + (double)rand() / (double)RAND_MAX * (SIZE_HIGH - SIZE_LOW);
cubeOrigins[i].rotateX = rand() % 2;
cubeOrigins[i].rotateY = rand() % 2;
if (cubeOrigins[i].rotateX == 0 && cubeOrigins[i].rotateY == 0)
{
cubeOrigins[i].rotateX = 1;
}

}

}

``````

I have commented out the line:

``````
//cubeOrigins[i].z = Z_LOW + (float)rand() / (float)RAND_MAX * (Z_HIGH - Z_LOW);

``````

because I know I will need this and this will start my cubes at different z-axis. My range is:

``````
const GLfloat Z_HIGH = 120.0;
const GLfloat Z_LOW = 0.0;

``````

The problem I am having is the movement. As you can see above I have cubeZ which is where the cubes start in the z plane and move out of view than starts over. I really do not know how to get the values of cubeOrgins[i].z into the myDisplay() function. I am hope someone can show me what I need to do the get them started at different points and moving out of the view. As one moves out of view, it needs to start again. I hope someone can help me and I appreciate all the help I can get. Thank You all!

Modify initRandoms() to take an index and only initialize the one cube at the given index. At program start you call this in a loop for all indices. After displaying a cube you update its z coordinate and if that cube’s z is above the threshold you call initRandoms again with the cube’s index to recycle it.

Also don’t swap buffers twice in you display callback, do it once after all drawing is done.