Tymo Tymo
Tymo Tymo

Reputation: 103

Do you have to call glfwSwapInterval(1) in an OpenGL program?

I ran my program twice. The first time with glfwSwapInterval(1) and everything was just fine. The second time without glfwSwapInterval(1) and it was using 100% of my CPU.

My Question: Is this normal and do I really have to call glfwSwapInterval(1) in order for my program to run properly.

The code:

glfwInit();

long window = glfwCreateWindow(1200, 800, "OpenGL", 0, 0);
glfwShowWindow(window);

glfwMakeContextCurrent(window);
GL.createCapabilities();

glClearColor(1, 0, 0, 1);

while (!glfwWindowShouldClose(window)) {
    glfwPollEvents();
    glClear(GL_COLOR_BUFFER_BIT);
    glfwSwapBuffers(window);
}
glfwTerminate();

Upvotes: 1

Views: 6392

Answers (2)

voithos
voithos

Reputation: 70552

The GLFW documentation mentions: "glfwSwapInterval is not called during context creation, leaving the swap interval set to whatever is the default on that platform."

So essentially, whether or not synchronization is enabled depends on your platform. For example, on my machine the default seems to be the opposite from what you were seeing.

In most cases, you'll want to call glfwSwapInterval(1) to enable sync, but if you have a reason to disable it (for example, if you're comparing shader performance) you can also call glfwSwapInterval(0).

Upvotes: 5

OutOfBound
OutOfBound

Reputation: 2004

If you want to sync your rendering loop to the refresh rate of the monitor you have to call it. The default behaviour is rendering as many frames as possible.

Upvotes: 0

Related Questions