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 & HelpSyntax Questions › Screencopy > display delayed...
Page Index Toggle Pages: 1
Screencopy > display delayed... (Read 1227 times)
Screencopy > display delayed...
Aug 17th, 2005, 11:39am
 
Hi!

Thanks to Mark Hill I figured out how to make a nasty mousefollow...now I am trying to have some visual effect in a way that I do not redraw the background every frame, but to have the previous frames fade away with time...so that movement trails become visual...
I was thinking that making a screencopy into an image via arraycopy and then displaying that copy with a blend should work...but it doesnt at all... How do the Pros do that Smiley

Code:

import processing.opengl.*;

PImage b;
PImage bgr;
BitmapFollow b1;
BitmapFollow b2;
BitmapFollow b3;

void setup()
{
size (500,500, OPENGL);
framerate(40);
background(255);

b = loadImage("http://processing.org/discourse/public_html/YaBBImages/smiley.gif");
bgr = new PImage(width, height);

b1 = new BitmapFollow(12);
b2 = new BitmapFollow(4);
b3 = new BitmapFollow(43);

b1.draw();
b2.draw();
b3.draw();

}

void draw()
{
blend(bgr, 0, 0,0,0, DARKEST);

b1.draw();
b2.draw();
b3.draw();

makeScreenCopy();
}


void makeScreenCopy()
{
loadPixels();
System.arraycopy(pixels,0,bgr.pixels,0,pixels.length);
bgr.updatePixels();
}


class BitmapFollow
{

float xm;
float ym;
int fric;
float a;
BitmapFollow(int f)
{
fric = f;
}

void draw()
{
pushMatrix();
xm += (mouseX - xm)/fric;
ym += (mouseY - ym)/fric;

translate(xm, ym);
rotate(atan2(mouseY-ym, mouseX-xm));

image(b, -b.width/2, -b.height/2);
popMatrix();
}
}

Re: Screencopy > display delayed...
Reply #1 - Aug 17th, 2005, 11:57am
 
A very quick solution for a fade over time effect is instead of bakcground(?) each frame, do:

Code:


fill(255, 80); // change these variables - 1st is grey value, and 2nd is opacity
noStroke();
rect(0,0,width, height);

Re: Screencopy > display delayed...
Reply #2 - Aug 17th, 2005, 12:19pm
 
Thanks again Mr. Hill Smiley

That is of course a very clever approach in this scenario, but what if I have a highly irregular, programmed background?
Then I guess I have no choice to making a screencopy...But how e.g. do I display the screencopy image with transparency...?

And somehow it doesnt make a perfect white, when I use e.g.:
fill( 255,255,255,30);
There is always a hint of the original image in it...how come that?
Re: Screencopy > display delayed...
Reply #3 - Aug 17th, 2005, 12:59pm
 

by using blend()

http://as.processing.org/reference/blend_.html

You can screen copy to a PImage buffer and then use this function, changing the alpha as you go.

The fade is another issue to do with bits left over when fading. This can be resolved by implementing a dedicated fade routine. I pulled this one from the Alpha board and it tackles precisely this problem. It may need some changing to run on Beta - PImage instead of BImage etc., but should get you what you need



Code:


int shift = 4;
int currentR, currentG, currentB;
int diffR, diffG, diffB;
int changeR, changeG, changeB;
int newR, newG, newB;
int targetR, targetG, targetB;

void fadescr(BImage source, color targetCol) {

targetR = (int)red(targetCol);
targetG = (int)green(targetCol);
targetB = (int)blue(targetCol);

int mindiff = int(pow(2,shift));

for (int i = 0; i < source.pixels.length; i++) {
// 1. Get colour compnents
// (same as calling red()/green()/blue(),
// but faster if in colorMode(RGB,255), and using ints)
currentR = (source.pixels[i] >> 16) & 0x000000ff;
currentG = (source.pixels[i] >> 8) & 0x000000ff;
currentB = source.pixels[i] & 0x000000ff;
// 2. Get difference between current and target
diffR = abs(targetR - currentR);
diffG = abs(targetG - currentG);
diffB = abs(targetB - currentB);
// 3. Calculate change as a fraction of the difference (using the shift value from above)
changeR = abs(diffR) >= mindiff ? diffR >> shift : diffR > 0 ? 1 : diffR < 0 ? -1 : 0;
changeG = abs(diffG) >= mindiff ? diffG >> shift : diffG > 0 ? 1 : diffG < 0 ? -1 : 0;
changeB = abs(diffB) >= mindiff ? diffB >> shift : diffB > 0 ? 1 : diffB < 0 ? -1 : 0;
// 4. Calculate the new colour
newR = currentR + changeR;
newG = currentG + changeG;
newB = currentB + changeB;
// 5. Put the components back together again
// (same as color(newR,newG,newB) in colorMode(RGB,255), but faster with ints
source.pixels[i] = (newR << 16) | (newG << 8) | newB;
}
}

Re: Screencopy > display delayed...
Reply #4 - Aug 17th, 2005, 4:47pm
 
Gosh, I really dont get it.


My simple idea is:

- make "Screenshot"
- set "Screenshot" with 10% opactiy on top of the until now rendered images.
-draw something on top

The fadescr function transforms an image to a certain color, whereas the intensity depends on the value of shift, so I could fade the screenshot (is there a very fast way to make a screenshot?) to white, and then blend it over the current picture. But it doesnt work out for me Sad
Could someone give me a hand please.


Additionally to that I get a weird error when I use some code from the reference regarding PImage.alpha();
Code:

size(500,375);

PImage img = loadImage("a.jpg");
PImage alphaImg = loadImage("b.jpg");
img.alpha(alphaImg);

image(img, 25, 0);


// ERROR:Semantic Error: No accessible method with signature "alpha(processing.core.PImage)" was found in type "processing.core.PImage".


But actually I am running version 91 so it should be there, right?
Re: Screencopy > display delayed...
Reply #5 - Aug 17th, 2005, 6:51pm
 
nope, alpha() is now mask() as of beta:
http://processing.org/reference/PImage_mask_.html
i'll add it to the faq on updating alpha -> beta code
Re: Screencopy > display delayed...
Reply #6 - Aug 19th, 2005, 8:58pm
 
Thanks for clearing that out! Especially as a newbie its quite hard to find these things... Smiley
Page Index Toggle Pages: 1