Flickering animation

Hi,

I’ve got a little problem with JOGL. I wanted to learn it so I have written a demo application which animates a colored triangle.

The main thread calls via run() update() method and GLCanvas.display() in loop. update() only changes X coord of one of the triangle’s vertices; the method responsible for drawing is display(GLAutoDrawable gLAutoDrawable) from GLEventListener.

It runs OK, but when I add a delay to the animation loop, it starts to flicker. And I don’t know why. I tried to add synchronized blocks into update() and display(), but it doesn’t help.

Here is the code, it’s simple:


package cz.wanto.jogltest;

import java.awt.*;
import javax.swing.*;
import javax.media.opengl.*;

/**
 *
 * @author wanto
 */
public class JoglTest implements GLEventListener {
    
    private JFrame frame;
    private GLCanvas canvas;
    
    /** Creates a new instance of JoglTest */
    public JoglTest() {
        frame = new JFrame("Jogl test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout());
        canvas = new GLCanvas();
        canvas.setPreferredSize(new Dimension(640, 480));
        canvas.addGLEventListener(this);
        frame.getContentPane().add(canvas);
        frame.pack();
        frame.setVisible(true);
    }
    
    public void run() {
        long currentTime = System.currentTimeMillis();
        while (true) {
            long elapsedTime = System.currentTimeMillis() - currentTime;
            currentTime += elapsedTime;
            
            update(elapsedTime);
            canvas.display();
            
            // I think this delay causes flickering
            try {
                Thread.sleep(20);
            }
            catch (InterruptedException e) {}
        }
    }
    
    float x1 = 320;
    boolean xGrowing = true;
    
    public void update(long elapsedTime) {
        // System.out.println("Update: " + Thread.currentThread().getName());
        if (xGrowing) {
            x1 += 0.5 * elapsedTime;
            if (x1 > 640) xGrowing = false;
        }
        else {
            x1 -= 0.5 * elapsedTime;
            if (x1 < 0) xGrowing = true;
        }
    }
    
    public void draw(GL gl) {
        // System.out.println("Draw:   " + Thread.currentThread().getName());
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
        gl.glBegin(GL.GL_TRIANGLES); {
            gl.glColor3f(1, 0, 0);
            gl.glVertex2f(x1, 0);
            gl.glColor3f(0, 1, 0);
            gl.glVertex2f(0, 479);
            gl.glColor3f(0, 0, 1);
            gl.glVertex2f(639, 479);
        } gl.glEnd();
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        JoglTest test = new JoglTest();
        test.run();
    }

    public void init(GLAutoDrawable gLAutoDrawable) {
        GL gl = canvas.getGL();
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrtho(0, 640, 0, 480, -1, -1);
        gl.glScalef(1 / (float)(640 / 2), -1 / (float)(480 / 2), 1);
        gl.glTranslatef(-(640 / 2), -(480 / 2), 0);
    }

    public void display(GLAutoDrawable gLAutoDrawable) {
        draw(canvas.getGL());
    }

    public void reshape(GLAutoDrawable gLAutoDrawable, int i, int i0, int i1, int i2) {
    }

    public void displayChanged(GLAutoDrawable gLAutoDrawable, boolean b, boolean b0) {
    }
    
}

Do you know how to solve this problem? Thanks for all answers,

Wan-To

Hi,

Why don’t you try the Animator or FPSAnimator for you animation? Give it a try by having a look to the jogl demos.

Edit: SOLVED!

It tourned out that a timeout of 50 milli seconds and a rotation of 10 degrees actuallay caused the “flickering”, when i switched to a timeout of 5 milli seconds and a rotation of 1 degree it smoothend the drawing. Sorry for my beginners fault :wink:

Hello community,

I encounter the same issue using Jogl 1.1.1. When putting the update inside the render loop everything seems to be fine, but if I start to outsource animation into another thread with a delay (sleep) the canvas begins to flicker. I also tried to use the FPSAnimator with multiples of my its refresh rate without any effect. Neither synchronizing on single objects in the scene graph nor locking during the entire render and animation processes did change a thing.

Is there any solution or suspicion on the cause of this isssue?

Regards Jakob

Edit:

My setup is another:

I USE the Animator and have another thread invoking the animation, only the latter is sleeping.