How to enable VSync synchronization in processing 2.x?

旧街凉风 提交于 2019-12-21 21:39:41

问题


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.


回答1:


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();
}


来源:https://stackoverflow.com/questions/20551224/how-to-enable-vsync-synchronization-in-processing-2-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!