How to make smooth movements in OpenGL?

So, this question is on the subject for my other OpenGL question (not my question with OpenGL ES, but OpenGL is the desktop version). If you have someone, press the key to move the square, how do you make the square movement natural and less jumping, but also at the same speed as now? This is my code for the glutKeyboardFunc () function:

void handleKeypress(unsigned char key, int x, int y) 
{
        if (key == 'w')
        {
            for (int i = 0; i < 12; i++)
            {
                if (i == 1 || i == 7 || i == 10 || i == 4)
                {
                    square[i] = square[i] + 1;
                }
            }
            glutPostRedisplay();
        }
        if (key == 'd')
        {
            for (int i = 0; i < 12; i++)
            {
                if (i == 0 || i % 3 == 0)
                {
                    square[i] = square[i] + 1;
                }
            }
            glutPostRedisplay();
        }
    if (key == 's')
    {
        for (int i = 0; i < 12; i++)
        {
            if (i == 1 || i == 7 || i == 10 || i == 4)
            {
                square[i] = square[i] - 1;
            }
        }
        glutPostRedisplay();
    }
        if (key == 'a')
        {
            for (int i = 0; i < 12; i++)
            {
                if (i == 0 || i % 3 == 0)
                {
                    square[i] = square[i] - 1;
                }
            }
            glutPostRedisplay();
        }
}

Sorry, if this is not entirely clear, I will try to rephrase it better if it does not make sense.

+3
source share
1 answer

If I understand correctly, the problem is that you change the position of the object on the keyboard event and request a display after this event processing.

, . .

, , ( , ) .

+4

Source: https://habr.com/ru/post/1744851/


All Articles