C++ - Movement on the X-axis (animation) - Opengl

Asked

Viewed 790 times

0

I have been trying to make a simple animation on the x-axis, where I have a chair and I have it drawn by several cubes, and in all these cubes I intend to move the x-variable of them, so that they move in that direction.

He’s already moving, what happens is I have to do some sort of iteration with the keyboard or the mouse to get it moving. As is the case with Initial Image to the Image After Movement. I wanted this to happen, but without some kind of user iteration being required.

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

Initial Image: Primeira Parte Image After Movement: Segunda Parte Final Image: Terceira Parte

Code I made for the chair to make 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 anyone knows how to help me in a way that I don’t need interact and the animation happens in the same thank you. If someone knows how can I make the chair not disappear at the end of the animation, would also be grateful.

Greetings and Thank You.

1 answer

1


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

Tip: You’re using a very old version of OpenGL which is not recommended to use, seek to research a little about Opengl profile core, or OpenGL programmable pipeline

  • I really needed to do in this version, since that’s what I was asked to do, but I have even finished the project and I have already solved this problem and its resolution was actually through adding some glFlush () .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.