Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Trying to "reset" the screen after cube moves out of picture

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2013
    Posts
    5

    Trying to "reset" the screen after cube moves out of picture

    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:

    Code :
    #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;
    }

  2. #2
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    474
    Update cubeZ in the idle callback, just like with rotateY.

  3. #3
    Junior Member Newbie
    Join Date
    Oct 2013
    Posts
    5
    Quote Originally Posted by GClements View Post
    Update cubeZ in the idle callback, just like with rotateY.
    Sweet! Thank you very much! Love this board.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •