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.
Page Index Toggle Pages: 1
flicker problem (Read 1130 times)
flicker problem
Jul 16th, 2007, 6:43pm
 
I am attempting to create a black/white random noise pattern ("static"). The size of the animation must be 800x600. Generating this number of random pixles without bogging down the framerate has been a challenge, but Processing is able to handle it nicely. The problem is a troublesome flicker that I have been unable to get rid of.

Any suggestions on how to improve the code would be appreciatd!

Code:


int sx, sy, x, y, bright;

void setup()
{
size(800,600);
sx = width;
sy = height;
}

void draw()
{
loadPixels();
for(x=0; x<sx; x=x+1) {
for(y=0; y<sy; y=y+1) {
int rn = (int) (Math.random() * 3);
bright = (rn%2 == 0) ? 255 : 0;
pixels[x+y*width] = color(bright);
}
}
updatePixels();
}

Re: flicker problem
Reply #1 - Jul 16th, 2007, 6:59pm
 
draw your noise on a image and then draw all of it to the screen.. its called double buffering or as i sometimes call it. offscreen rendering.. if you keep drawing directly to screen output, you will noticed flicker coz of the refresh rate and vsync
Re: flicker problem
Reply #2 - Jul 16th, 2007, 8:13pm
 
As an aside, I think I can pretty much double the speed of your noise generation:

Change:
Code:
  int rn = (int) (Math.random() * 3);
bright = (rn%2 == 0) ? 255 : 0;
pixels[x+y*width] = color(bright);

to
Code:
pixels[x+y*width] = 0xFFFFFFFF ^ ((((int)random(3))%2) * 0x00FFFFFF);


(BTW, you do realise you've got a 2:1 ratio of white:black pixels? If you want to even it out, it's just (int)random(2) and no need for the %2.)
Re: flicker problem
Reply #3 - Jul 17th, 2007, 2:02am
 
Thanks for the input guys. The suggested changes have greatly improved the sketch, but unfortunately the flicker remains. I am new to Processing, but after poking around here a bit I put together the below code that I *think* implements double-buffering. Please let me know if this is correct, and if you have any additional thoughts on this.

Thanks!

Code:

PGraphics g;
int sx, sy, x, y;

void setup()
{
size(800, 600, P3D);
sx = width;
sy = height;
g = createGraphics(800,600, P3D);
}

void draw()
{
g.beginDraw();
g.loadPixels();

for (x = 0; x < sx; x=x+1) {
for (y = 0; y < sy; y=y+1) {
g.pixels[x+y*width] = 0xFFFFFFFF ^ ((int)random(2) * 0x00FFFFFF);
}
}
g.updatePixels();
g.endDraw();
image(g,0,0);
}
Re: flicker problem
Reply #4 - Jul 17th, 2007, 10:53am
 
what you're talking about is bad framerate.. you application is running really slow. why dont you just render a couple of noise images and then use those to simulate the effect? that would work just as nice and alot cheaper
Page Index Toggle Pages: 1