Hello everyone.
I am working on a program that has a 3d cube rotating and also moving along the z-axis out of the screen. Once it is out of the screen, it needs to start over again. I thought that using a conditional statement might do the trick. Apparently I am wrong or I am just doing it wrong. Can someone show me what I am doing wrong and how to correct it?
Here is what I have so far:
#include <Windows.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
/* -- GLOBAL VARIABLES ----------------------------------------------------------------------------------------- */
GLint screenWidth = 500;
GLint screenHeight = 500;
GLfloat cubeX = 0.0;
GLfloat cubeY = 5.0;
GLfloat cubeZ = 0.0;
GLfloat rotateY = 0.0;
//GLfloat rotCube = 0.0;
GLint cubeCount = 0;
//const GLint CUBE_LOW = 7;
//const GLint CUBE_HIGH = 15;
const GLint CUBE_HIGH = 15;
const GLint CUBE_LOW = 7;
const GLfloat X_HIGH = 10.0;
const GLfloat X_LOW = -10.0;
const GLfloat Y_HIGH = 10.0;
const GLfloat Y_LOW = -10.0;
//bool offScreen = false;
/* -- NAMESPACE ------------------------------------------------------------------------------------------------ */
using namespace std;
/* ------------------------------------------------------------------------------------------------------------- */
/*
Function: void rotateCube()
*/
void rotateCube()
{
rotateY += 0.050f;
if(rotateY > 360.0f)
rotateY -= 360.0f;
glutPostRedisplay();
}
/* ------------------------------------------------------------------------------------------------------------- */
/*
Function: void myDisplay()
*/
void myDisplay()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-2.0, 2.0, -20.0);
glTranslatef(2.0, 2.0, cubeZ);
glRotatef(rotateY, 0.0f, 1.0f, 0.0f);
glutWireCube(2.0f);
cubeZ += 0.010f;
glutSwapBuffers();
glFlush();
if (cubeZ > 20.0f)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-2.0, 2.0, -20.0);
glTranslatef(2.0, 2.0, cubeZ);
glRotatef(rotateY, 0.0f, 1.0f, 0.0f);
glutWireCube(2.0f);
cubeZ += 0.010f;
glutSwapBuffers();
glFlush();
}
}
/* ------------------------------------------------------------------------------------------------------------- */
/*
Function: int main( int argc, char **argv )
*/
void myReshape(int width, int height)
{
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100);
glMatrixMode(GL_MODELVIEW);
}
/* ------------------------------------------------------------------------------------------------------------- */
/*
Function: int main( int argc, char **argv )
*/
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(screenWidth, screenHeight);
glutInitWindowPosition(100, 40);
glutCreateWindow("Boxes!");
glutDisplayFunc(myDisplay);
glutIdleFunc(rotateCube);
glutReshapeFunc(myReshape);
glutMainLoop();
return 0;
}