C ++ - X-axis motion (animation) - OpenGL

0

I've been trying to do a simple animation on the x-axis, where I have a chair and I've drawn it by several cubes, and in all those cubes I want to move the x-variable of them so they can move in that direction.

He is already moving what happens is that I have to do some type of iteration with the keyboard or the mouse so that it moves. Just like the Start Image for the Image After Motion . I wanted this to happen, but without some kind of iteration on the part of the user.

When the chair comes close to the table it reaches the coordinate limit, and has to go back, what happens is that it goes back once 0.05, and then disappears, as it is in the Final Image >.

Initial Image: ImageAfterMotion: FinalImage:

Code I made for the chair to do this animation:

float animation()
{
    //glFlush();
    for(;varX < 1;)
    {
            varX += 0.05;
            return varX;
    }

    if (varX >= 1)
    {
        for(;varX == 0;)
        {
            returnPosition();
            return varX;
        }
    }
    //glutPostRedisplay();

    /*
    do
    {
        varX += 0.05;
        return varX;
    }while(varX < 1);
    */
}

float returnPosition()
{
    //glFlush();
     for(;varX != 0;)
    {
            if(varX < 0)
            {
                for(;varX == 0;)
                {
                    varX += 0.05;
                    return varX;
                }

            }
            if(varX > 0)
            {
                for(;varX == 0;)
                {
                    varX -= 0.05;
                    return varX;
                }
            }
    }
    //glutPostRedisplay();
}

These functions are being called in the following function:

void display(void)
{

    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (0.32, 0.32, 0.32); // Achei esta cor RGB em: http://www.rapidtables.com/web/color/RGB_Color.htm
    glLoadIdentity ();
    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);


    glTranslatef(0.0, 0.0, zoom /*zoom*/);
    glRotatef(rotateX, 2.0, 0.0, 0.0);
    glRotatef(rotateY, 0.0, 2.0, 0.0);
    glRotatef(rotateZ, 0.0, 0.0, 2.0);

    anim = animation();


    chair(0, 0, 0, anim);
    chair(0, 0, 1, 0);
    table(0.05, 0.05, 0.05);

    glFlush ();

}
  

If someone knows how to help me so that I do not need to   interact and the animation happens in the same thanks. If anyone knows   how can I do it so that the chair does not disappear at the end of   animation, I would also be grateful.

Best wishes and thanks.

    
asked by anonymous 28.05.2016 / 20:08

1 answer

1

By analyzing your code, it seems to me that you are not drawing the code by changing the values of eixo X , every time you change the value of the X axis, you need to call glFlush() , then in your case, the change occurs only in memory, and only in the last position that you calculate is it runs glFlush()

Tip: You are using a very old version of OpenGL which is not recommended for use, look for a little bit about OpenGL profile core, or OpenGL programmable pipeline

    
28.09.2016 / 21:50