user680725
user680725

Reputation: 123

OpenGL Vertical Sync Makes Rendering Faster

I noticed that when I render a simple primitive, like a rotating Cube, and I increase the angle by 0.1 each pass through the traditional while (!done) loop, the result I get when vertical sync is on versus when it is off (as changed in my nVidia Control Panel) is drastically different in terms of speed.

For example, I use the following code:

while (!done)
{
    PeekMessage(&msg, hWnd, NULL, NULL, PM_REMOVE);

    if (msg.message == WM_QUIT)
    {
        done = true;
    }
    else
    {
        angle += 0.1;

        if (angle >= 360.0)
        {
            angle = 0.0;
        }

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();         

        glTranslatef(0.0f, 0.0f, -5.0f);

        glPushMatrix();
            glRotatef(angle, 1.0f, 0.0f, 0.0f);
            glRotatef(angle, 0.0f, 1.0f, 0.0f);
            glColor3f(1.0f, 0.0f, 0.0f);
            glutSolidCube(2.0f);
        glPopMatrix();

        glFlush();
        SwapBuffers(hDC);

        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

When vertical sync is forced off, this cube rotates a lot faster than when vertical sync is forced on. Now, in games, the FPS is locked to 60 fps when vertical sync is enabled, and free to jump as high as your monitor allows when it is disabled. This probably has something to do with it.

I can easily fix this issue by increasing or decreasing my angle with each pass, is there a way to get my cube to rotate the same way despite vsync being on or off?

Upvotes: 0

Views: 487

Answers (1)

ronag
ronag

Reputation: 51283

Yes, use a time delta. Instead of rotating 0.1 every frame, you would want to rotate 0.1 every 20 ms or something. That way it will rotate with the same speed regardless of the fps.

angle += 0.1 * time_elapsed_since_last_tick * some_factor;

Upvotes: 5

Related Questions