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 › Strange reference problems with webcam and opengl
Page Index Toggle Pages: 1
Strange reference problems with webcam and opengl (Read 782 times)
Strange reference problems with webcam and opengl
Jan 23rd, 2006, 7:37pm
 
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();
   }  
}
Re: Strange reference problems with webcam and ope
Reply #1 - Jan 24th, 2006, 2:26am
 
current.pixels = camera.pixels will only copy the reference to the data, not the actual data, so that may be a problem (you need to use arraycopy() in that case)

when using a PGraphics3 as a texture, you'll also need to call updatePixels() so that the texture updates properly.

other than that, if it's truly showing up in random places (like if you load a font and some of the characters are a webcam image) then you might have a texture binding problem which might require another solution...
Re: Strange reference problems with webcam and ope
Reply #2 - Jan 24th, 2006, 11:14am
 
Thak you very much, Fry Smiley. updatePixels() did the trick, i thought this is done automatically. Thanks again, both for the advice and for wonderful Procesing Smiley
Page Index Toggle Pages: 1