I am trying to put webcam image onto polygons as a texture so that i can morph image quickly etc.
The problem is, that i created a few Pgraphics3 images to use as buffer for camera so that i can compute difference image between two frames (i dont think that pgraphics3 is problem as it works the same with PImage). The 'bug' I encounter is that processing puts the image from camera into very random places - in the following program, i copy pixels from camera into one of the buffers called current with current.pixels=camera.pixels; command (makes same bug when copying pixel-by-pixel in a loop). But after that, the 'camera' image updates every tenth frame, while when i texture polygon with 'diff' image (which is empty and i haven't done anything with it after creating), it contains smooth video that should have been in 'camera' and 'current' variables (how strange - how did it get there???).
Can anyone please point me in a right direction?
Thanks!
Code:
import processing.opengl.*;
import processing.video.*;
int camx=320; int camy=240;
PGraphics3 last = new PGraphics3(camx,camy,null);
PGraphics3 current = new PGraphics3(camx,camy,null);
PGraphics3 diff = new PGraphics3(camx,camy,null);
boolean newFrame;
Capture camera;
void setup()
{
size(800,600, OPENGL);
noStroke();
camera = new Capture(this, camx, camy, 30);
newFrame=false;
}
void captureEvent(Capture camera)
{
camera.read();
newFrame=true;
}
void draw()
{
if (newFrame==true) {
newFrame=false;
/* //THIS WORKS THE SAME
for (int i = 0; i < (camx*camy); i++){
current.pixels[i]=camera.pixels[i];
}
*/
current.pixels=camera.pixels;
//there will be a lot more going on here, but after i get this running
}
float x = mouseX - width/2;
background(255);
translate(0,0);
//rotateZ(radians((mouseY-height/2)/10));
int ctverec=50;
float presahX=2*((mouseX - width/2)/1.33);
float presahY=2*((mouseY - height/2));
textureMode(NORMALIZED);
for(float i=0; i<height; i+=ctverec) {
for(float j=0; j<width; j+=ctverec) {
beginShape(QUADS);
texture(camera);
vertex( (j) ,(i) , 0, (j-presahX)/width , (i-presahY)/height);
vertex( (j) ,(i)+ctverec , 0, (j-presahX)/width , (i+ctverec+presahY)/height);
vertex( (j)+ctverec ,(i)+ctverec , 0, (j+ctverec+presahX)/width , (i+ctverec+presahY)/height);
vertex( (j)+ctverec ,(i) , 0, (j+ctverec+presahX)/width , (i-presahY)/height);
endShape();
}
}
if(keyPressed) {
if (key == 's' || key == 'S') { //saving
String filename;
filename=("grk_"+String.valueOf(year())+String.valueOf(month())+String.valueOf(day())+'-'+String.valueOf(hour())+String.valueOf(minute()+String.valueOf(second())));
save(filename);
println("Saved "+filename+"...");
}
} else {
//only draw if we aren't saving
//preview of camera
beginShape(QUADS);
texture(camera); // THIS DRAWS CAMERA'S IMAGE AT ABOUT 1 FPS???
vertex(0 , 0, 0, 0, 0);
vertex(0 ,120, 0, 0, 1);
vertex(160,120, 0, 1, 1);
vertex(160, 0, 0, 1, 0);
endShape();
//preview of diff - difference image
beginShape(QUADS);
texture(diff); // THIS ACTUALLY DRAWS SMOOTH CAMERA'S IMAGE - WHERE THE HECK HAS IT GOT THERE???
vertex(160, 0, 0, 0, 0);
vertex(160,120, 0, 0, 1);
vertex(320,120, 0, 1, 1);
vertex(320, 0, 0, 1, 0);
endShape();
}
}