We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › OPENGL code (flickering error)
Page Index Toggle Pages: 1
OPENGL code (flickering error) (Read 2466 times)
OPENGL code (flickering error)
Jan 22nd, 2009, 10:00am
 
Hi,
I have been mostly using P3D.
Today I decided to run one of my P3D sketch in OPENGL mode.

When I did that, the code flickers a lot and junk showed on my screen.

So I downloaded a OPENGL sketch from Processing website and it worked perfectly fine.

So I modified my sketch to use beginShape() and endShape() instead of line() and point()

Still the code flickers and shown junk.

Any idea what I am doing wrong?

Here's my code:

Quote:
import processing.opengl.*;

// 
float len = random(220);
float radius1 = random(20, 130);
float radius2 = random(10, 100);
float position = random(101);
float num = random(5, 10);
float prevX = 0.0;
float prevY = 0.0;
float x = 0.0;
float y = 0.0;
// 
PVector point1, point2;
// 
float step = 1 * PI / 180.0;
float theta = 0;
int i=0;
float count = 0;
 
// 
void setup(){
  size(500, 500, OPENGL);
  background(255);
  stroke(0, 20);

 
void draw(){
    count+=1; 
    translate(width/2, height/2);
      prevX = x; 
      prevY = y; 
      x = len * cos(theta) - position * cos(len * theta / radius2);
      y = len * sin(theta) - position * sin(len * theta / radius2);
      //
      point1 = new PVector(prevX, prevY);
      point2 = new PVector(x, y);
      //
      theta += step; 
      i++; 
      if(prevX!=0){
        stroke(0);
        strokeWeight(1);
       PVector perp = new PVector(point2.y - point1.y, point1.x - point2.x);
       // Length 1
        perp.normalize();
        // Length to n pixels
        perp.mult(60);  // Put 10 for 10 pixels
        // Move to point1
        perp.add(point1);
        // Draw it
        stroke(#AA2244);
        strokeWeight(1);
        beginShape(LINES);
        vertex(point1.x, point1.y);
        vertex(perp.x+random(-5, +5), perp.y+random(-5, +5));
        endShape();
      } 

void mousePressed(){
  background(255);
  initL(); 

 
void initL(){
  len = random(220);
  radius1 = random(20, 130);
  radius2 = random(10, 100);
  position = random(101);
  num = random(5, 10);
  prevX = 0; 
  prevY = 0; 
  x = 0; 
  y = 0; 
}


Re: OPENGL code (flickering error)
Reply #1 - Jan 22nd, 2009, 2:45pm
 
In your draw( ) loop, you need to clear the background before drawing the
next frame.

void draw( )
{
  background( 255 );
  ... rest of your code ....
}

The junk should dissapear. If by flickering, you mean tearing in the screen,
then you need to enable vertical sync. What that will do is tie your frameRate
with the monitor refresh rate.

This function will do that for you, it's called once (from setup) to enable
vertical sync. If you wish to toggle it you can set the swap interval to 0
at any time to disable it again.

void enableVSync()
{
 GL pgl = (PGraphicsOpenGL)g;
 gl = pgl.beginGL( );
 gl.setSwapInterval( 1 ); // use value 0 to disable v-sync
 pgl.endGL( );
}

Hope this helps.
Re: OPENGL code (flickering error)
Reply #2 - Jan 22nd, 2009, 5:39pm
 
Hi,
Does it mean that you can not do recurcive drawings using OPENGL?
Re: OPENGL code (flickering error)
Reply #3 - Jan 23rd, 2009, 9:58am
 
No ofcourse not, if you want to draw over the previous screen simply do not
clear it. But then, that junk you were talking about must be something else.
Can you post a screenshot?
Re: OPENGL code (flickering error)
Reply #4 - Feb 9th, 2009, 8:39am
 
The worst flickering problems I get are because vsync isn't enabled by default. Maybe that' s your problem? Your code worked perfectly fine for me though.

Anyways I found an example that enables vsync, and it solved all my problems in life. Just copy in the code you need from this:

import processing.opengl.*;
import javax.media.opengl.GL;

PGraphicsOpenGL pgl;
GL gl;

void setup()
{
 size(400,400, OPENGL);
 pgl = (PGraphicsOpenGL) g; //processing graphics object
 gl = pgl.beginGL(); //begin opengl
 gl.setSwapInterval(1); //set vertical sync on
 pgl.endGL(); //end opengl
}

Re: OPENGL code (flickering error)
Reply #5 - Jul 2nd, 2009, 8:47pm
 
Not to bring back a topic that's been discussed to death, but I found today that enabling/disabling the Aero theme in Vista can fix the flickering / tearing in opengl applets.

Tried the above code (setSwapInterval with 0 and 1) in Vista Basic:
Both had tearing, no matter the framrate (fast or slow)

Tried with setSwapInterval 0 or 1 in Aero
Neither tore, even up to 120 fps!

Hmm... Must be my weird graphics drivers.
Page Index Toggle Pages: 1