anorm
anorm

Reputation: 2263

How to enable VSync synchronization in processing 2.x?

Previously, in processing 1.x, I used the following code to enable VSync synchronization:

void enableVSync()
{
    frameRate(-1);
    GL pgl = (PGraphicsOpenGL)g;
    gl = pgl.beginGL();
    gl.setSwapInterval(1);
    pgl.endGL();
}

This does not work in processing 2.x and I can't seem to find out how or even if it is supposed to work in processing 2.x.

Edit:

By switching from size(500, 500); to size(500, 500, P2D);, it seems to help. It now looks like processing does all the drawing in a back buffer and switches it to the front buffer at the VSync.

However, the draw() function is still asynchronous with the vsync and even though I don't see any tearing anymore, there's still motion stuttering whenever a frame is skipped or drawn twice.

Upvotes: 2

Views: 2098

Answers (1)

anorm
anorm

Reputation: 2263

Turns out, frameRate() in PJOGL actually runs setSwapInterval() though with some strange logic regarding the value set (github). A workaround for this is:

void setup()
{
    setup(500, 500, P2D);
    frameRate(-1);                                      // set unlimited frame rate
    ((PJOGL)PGraphicsOpenGL.pgl).gl.setSwapInterval(1); // enable waiting for vsync
                                                        // before swapping back/front buffers
}

EDIT:

For Processing 3, I use the following:

import java.awt.*;
import javax.media.opengl.glu.GLU;

void setup()
{
    frameRate(-1);
    beginPGL();
    GLU.getCurrentGL().getGL2().setSwapInterval(1);
    endPGL();
}

EDIT 2:

For Processing 3.2, the following seems to work:

void setup()
{
  fullScreen(P3D);
  frameRate(1000);
  PJOGL pgl = (PJOGL)beginPGL();
  pgl.gl.setSwapInterval(1);
  endPGL();
}

Upvotes: 2

Related Questions