FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   PuSHpopTransform
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: PuSHpopTransform  (Read 428 times)
skloopy

WWW
PuSHpopTransform
« on: Apr 7th, 2003, 6:20am »

Hi thanks for the help on the other questions. Here's another :^P. This works fine until i uncomment the push() pop() and transform() commands in the smile() method.
 
It seems like as soon as i start using one of those it messes up the beginShape(XXX) type commands from then on. Any ideas??
 
Code:
int frame;
float prmouseX, prmouseY;
 
FlowDistort fd;
 
color cColor;
 
void setup() {
  size(300, 250);
   
  fd = new FlowDistort(width, height, 6, 6);
  cColor = color(255,0,0);
   
  prmouseX = mouseX;
  prmouseY = mouseY;
   
  noBackground();
  hint(SMOOTH_IMAGES);
  ellipseMode(CENTER_DIAMETER);
}
 
void loop() {
  cColor = color(red(cColor)+random(-50,50),  
       green(cColor)+random(-50,50),  
       blue(cColor)+random(-50,50));
  fill(cColor);
  stroke(cColor);
   
  smooth();
  strokeWidth(5);
  line(mouseX,mouseY,prmouseX,prmouseY);
  strokeWidth(1);
  noStroke();
  ellipse(mouseX, mouseY, 10,10);
  ellipse(prmouseX, prmouseY, 10,10);
  noSmooth();
   
  prmouseX = mouseX;
  prmouseY = mouseY;
   
  //if ((int)random(10) == 0) {
    noFill();
    stroke(255,0,0);
    //strokeWidth(2);
    //smooth();
    smile(random(width), random(height), random(TWO_PI));
    //noSmooth();
    //strokeWidth(1);
  //}
   
  noStroke();
  fill(255,255,255,200);
  fd.update(pixels);
  fd.concentrate(mouseX, mouseY, .5f);
  fd.avgVec();
  fd.avgVec();
  fd.avgVec();
  fd.avgVec();
  fd.avgVec();
  fd.draw();
   
  stroke(255,0,0);
  //fd.drawVec();
  //fill(255,255,255,50);
  //image(fd.in, 0,0);
   
  frame++;
}
 
void smile(float x, float y, float r)
{
  //push();     // Uncomment
  //translate(x,y);  // Uncomment
  //rotate(r);  // Uncomment
   
  // Face
  beginShape(LINE_LOOP);
  for(int i=0; i<24; i++) {
    push();
    rotateY((TWO_PI / 24) * i);
    vertex(objectX(50,0,0),objectY(50,0,0));
    pop();
  }
  endShape();
 
  // Normal Eyes
  line(-20,-25,-20,-5);
  line(20,-25,20,-5);
 
  // Mouth
  bezier(-35,5,-30,45,30,45,35,5);
   
  //pop();  // Uncomment
}
 
//
// Class FlowDistort
//
class FlowDistort
{
  int width, height, nU, nV;
  float uWidth, vHeight;
  float vX[], vY[]; // Flow X and Y Vectors
  int nVecs;
   
  BImage in;
   
  FlowDistort(int w, int h, int nU, int nV) {
    width = w; height = h;
    this.nU = nU; this.nV = nV;
     
    nVecs = nU * nV;
    vX = new int[nVecs];
    vY = new int[nVecs];
     
    uWidth = (float)w / (nU-1);
    vHeight = (float)h / (nV-1);
     
    in = new BImage(new int[width*height], width, height, RGB);
     
    /*for(int i=0; i < nVecs; i++) {
 vX[i] = random(-2,2);
 vY[i] = random(-2,2);
    }*/
  }
   
  void update(color pix[]) {
    System.arraycopy(pix,0,in.pixels,0,width*height);
  }
   
  void draw() {
    int nVMinusOne = nV - 1;
    int vIndex;
    float xBase, yBase;
     
    for(int j=0; j < nVMinusOne; j++) {
 beginShape(QUAD_STRIP);
 textureImage(in);
 for(int i=0; i < nU; i++) {
   vIndex = j * nU + i;
   xBase = uWidth * i;
   yBase = vHeight * j;
   if (i % 2 == 0) {
     vertexTexture(xBase, yBase);
     vertex(xBase + vX[vIndex], yBase + vY[vIndex]);
     vertexTexture(xBase, yBase + vHeight);
     vertex(xBase + vX[vIndex + nU], yBase + vHeight + vY[vIndex + nU]);
   } else {
     vertexTexture(xBase, yBase + vHeight);
     vertex(xBase + vX[vIndex + nU], yBase + vHeight + vY[vIndex + nU]);
     vertexTexture(xBase, yBase);
     vertex(xBase + vX[vIndex], yBase + vY[vIndex]);
   }
 }
 endShape();
    }
  }
   
  void drawVec() {
    int vIndex;
    for(int j=0; j < nV; j++) {
 for(int i=0; i < nU; i++) {
   vIndex = j * nU + i;
   line(uWidth * i, vHeight * j, uWidth * i + vX[vIndex], vHeight * j + vY[vIndex]);
 }
    }
  }
   
  void avgVec() {
    int vIndex;
    float xSum, ySum;
    for(int j=0; j < nV; j++) {
 for(int i=0; i < nU; i++) {
   vIndex = j * nU + i;
   xSum = vX[vIndex]; ySum = vY[vIndex];
   
   if (i != 0) {
     xSum += vX[vIndex-1];
     ySum += vY[vIndex-1];
   }
   if (i != nU-1) {
     xSum += vX[vIndex+1];
     ySum += vY[vIndex+1];
   }
   if (j != 0) {
     xSum += vX[vIndex-nU];
     ySum += vY[vIndex-nU];
   }
   if (j != nV-1) {
     xSum += vX[vIndex+nU];
     ySum += vY[vIndex+nU];
   }
   
   vX[vIndex] = xSum / 5.0f;
   vY[vIndex] = ySum / 5.0f;
 }
    }
  }
   
  void concentrate(float x, float y, float strength) {
    int vIndex;
    for(int j=0; j < nV; j++) {
 for(int i=0; i < nU; i++) {
   vIndex = j * nU + i;
   vX[vIndex] = (i*uWidth - x) * strength;
   vY[vIndex] = (j*vHeight - y) * strength;
 }
    }
  }
}
 
Martin

122417302122417302martingomez_listsmg1ph WWW Email
Re: PuSHpopTransform
« Reply #1 on: Apr 7th, 2003, 7:39am »

what version of p5 are u using? can't seem to run it on 0052
 
skloopy

WWW
Re: PuSHpopTransform
« Reply #2 on: Apr 7th, 2003, 9:14am »

I'm using 52. That's strange. I was going to post it on my site so you could take a look, but it errors as an applet.
 
See if it works of your computer:
http://elfman.vendetta.com/ryan/flowtest/
 
The code is here:
http://elfman.vendetta.com/ryan/flowtest/flowImage.pde
 
Maybe there was a problem with the code I posted, so maybe this code will work. I'm Using p52 on OS9 just so you know.
 
skloopy

WWW
Re: PuSHpopTransform
« Reply #3 on: Apr 7th, 2003, 9:49am »

Okay I made a change to the applet so is should work now.
 
benelek

35160983516098 WWW Email
Re: PuSHpopTransform
« Reply #4 on: Apr 8th, 2003, 12:18am »

whoa...
 
skloopy

WWW
Re: PuSHpopTransform
« Reply #5 on: Apr 17th, 2003, 11:04am »

Hmm I tried this problem in p54 with the same result. I'm running into this problem with switching 2d/3d coorinate systems in other places (I think that's what it is) so it'd be really helpful if someone knew what's doing this.. I can simplify the code if this is too much to look at.
 
thank u
 
fry


WWW
Re: PuSHpopTransform
« Reply #6 on: May 1st, 2003, 4:47pm »

yeah, if you can get it down to a shorter example, that would help (at least for me).  
 
i think there are two things that could be happening. first would be that you're just getting unexpected results from lack of familiarity with the transforms; the other would be that the graphics engine is messing with you.  
 
another thing to do would be to add a line saying "g.dimensions = 3" to see if that makes the weirdness show up (try both with/without those parts commented out) that will turn off the optimizations that are sometimes used internally if things aren't fully 3D.
 
skloopy

WWW
Re: PuSHpopTransform
« Reply #7 on: May 3rd, 2003, 8:22pm »

Here's a simpler version. I just figured it out. In the first example it was because I was using rotateY() instead of rotate() and it was somehow getting carried over despite the pop() call.
 
This works for just drawing the smile. I've just been rotating and using objectX and Y to make the circle (It was supposed to be deformable) but maybe there is a better way?
 
Anyways, it's another broblem you can chalk up to good ol' user error!
 
ryan
 
Code:
void setup() {
  size(200, 200);
  ellipseMode(CENTER_DIAMETER);
}
 
void loop() {
  noFill();
  smile(mouseX, mouseY, 0);
}
 
void smile(float x, float y, float r)
{
  push();     // Uncomment
  translate(x,y);  // Uncomment
  rotate(r);  // Uncomment
 
  // Face
  beginShape(LINE_LOOP);
  push();
  for(int i=0; i<24; i++) {
    rotate(TWO_PI / 24);
    vertex(objectX(50,0,0)-x,objectY(50,0,0)-y);
  }
  pop();
  endShape();
 
  // Normal Eyes
  line(-20,-25,-20,-5);
  line(20,-25,20,-5);
 
  // Mouth
  bezier(-35,5,-30,45,30,45,35,5);
 
  pop();  // Uncomment
}
 
Pages: 1 

« Previous topic | Next topic »