JOGL Double Buffering

graphics, java, jogl

Solution

If JOGL is like the C/C++ version:

RMorrisey and the sample code is incorrect in stating the use of glFlush.

The swapBuffers function must go at the end of the drawing.

To confirm this: have the shapes do animation very quickly and watch for tearing. If you get tearing then you are doing a single draw, if you don't then you are using double buffering.

Problem

What is eligible way to implement double buffering in JOGL (Java OpenGL)? I am trying to do that by the following code: ``` ... /** Creating canvas. */ GLCapabilities capabilities = new GLCapabilities(); capabilities.setDoubleBuffered(true); GLCanvas canvas = new GLCanvas(capabilities); ... /** Function display(…), which draws a white Rectangle on a black background. */ public void display(GLAutoDrawable drawable) { drawable.swapBuffers(); gl = drawable.getGL(); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glColor3f(1.0f, 1.0f, 1.0f); gl.glBegin(GL.GL_POLYGON); gl.glVertex2f(-0.5f, -0.5f); gl.glVertex2f(-0.5f, 0.5f); gl.glVertex2f(0.5f, 0.5f); gl.glVertex2f(0.5f, -0.5f); gl.glEnd(); } ... /** Other functions are empty. */ ``` Questions: — When I'm resizing the window, I usually get flickering. As I see it, I have a mistake in my double buffering implementation. — I have doubt, where I must place function swapBuffers — before or after (as many sources says) the drawing? As you noticed, I use function swapBuffers (`drawable.swapBuffers()`) before drawing a rectangle. Otherwise, I'm getting a noise after resize. So what is an appropriate way to do that? Including or omitting the line `capabilities.setDoubleBuffered(true)` does not make any effect.

Original source