Loading...
Logo
Processing Forum
can somebody help me how to add image texture over polygon object. I am stuck in this problem. Here is my code

// center point
float centerX = 0, centerY = 0;

float radius = 45, rotAngle = -90;
float accelX, accelY;
float springing = .0009, damping = .98;

//corner nodes
int nodes = 4;
float nodeStartX[] = new float[nodes];
float nodeStartY[] = new float[nodes];
float[]nodeX = new float[nodes];
float[]nodeY = new float[nodes];
float[]angle = new float[nodes];
float[]frequency = new float[nodes];

// soft-body dynamics
float organicConstant = 1;
PImage img;

void setup() {
  size(640, 360);
  //center shape in window
  img = loadImage("img.jpg");
  centerX = width/2;
  centerY = height/2;
  // iniitalize frequencies for corner nodes
  for (int i=0; i<nodes; i++){
    frequency[i] = random(5, 12);
  }
  noStroke();
  smooth();
  frameRate(30);
}

void draw() {
  //fade background
  fill(0, 100);
  rect(0,0,width, height);
  drawShape();
  moveShape();
}

void drawShape() {
  //  calculate node  starting locations
  for (int i=0; i<nodes; i++){
    nodeStartX[i] = centerX+cos(radians(rotAngle))*radius;
    nodeStartY[i] = centerY+sin(radians(rotAngle))*radius;
    rotAngle += 360.0/nodes;
  }

  // draw polygon
  curveTightness(organicConstant);
  fill(255);
  beginShape();
  //texture(img);
  for (int i=0; i<nodes; i++){
    curveVertex(nodeX[i], nodeY[i]);
  }
  for (int i=0; i<nodes-1; i++){
    curveVertex(nodeX[i], nodeY[i]);
  }
  endShape(CLOSE);
}

void moveShape() {
  //move center point
  float deltaX = mouseX-centerX;
  float deltaY = mouseY-centerY;

  // create springing effect
  deltaX *= springing;
  deltaY *= springing;
  accelX += deltaX;
  accelY += deltaY;

  // move predator's center
  centerX += accelX;
  centerY += accelY;

  // slow down springing
  accelX *= damping;
  accelY *= damping;

  // change curve tightness
  organicConstant = 1-((abs(accelX)+abs(accelY))*.1);

  //move nodes
  for (int i=0; i<nodes; i++){
    nodeX[i] = nodeStartX[i]+sin(radians(angle[i]))*(accelX*2);
    nodeY[i] = nodeStartY[i]+sin(radians(angle[i]))*(accelY*2);
    angle[i]+=frequency[i];
  }
}

Replies(3)

does anyone know how to do it. pls help guys
You can't use textures in combination with curveVertices. Only regular vertices allow you to specify texture coordinates. See the reference. So you have to explore alternative solutions:
  1. Use vertices instead. You can actually make very smooth shapes with triangulated surfaces.
  2. Use a quad but with a partly transparent texture.
  3. Use masks. See this solution by PhiLho.


thanks amnon.owed, actually I wanted that softbody moving effects( see image below) on the object on mousemoveut somehow I have not been able to get those effects.
   





I have used vertex function and got the texture on the object but moving effects are not there. Is there any way to get those effects.

here is my output image and code



Copy code
  1. import processing.opengl.*;

  2. // center point
  3. float centerX = 0, centerY = 0;
  4. float radius = 45, rotAngle = -90;
  5. float accelX, accelY;
  6. float springing = .0009, damping = .98;

  7. //corner nodes
  8. int nodes = 4;

  9. float[][] pointArray = new float[nodes][2];
  10. float[][] imgArray = new float[nodes][2];
  11. float[]nodeX = new float[nodes];
  12. float[]nodeY = new float[nodes];
  13. float[]angles = new float[nodes];
  14. float[]frequency = new float[nodes];

  15. // soft-body dynamics
  16. float organicConstant = 1;
  17. PImage img;

  18. void setup() {
  19.   size(640, 480, OPENGL);
  20.   //center shape in window
  21.   centerX = width/2;
  22.   centerY = height/2;
  23.   img = loadImage("img.jpg");
  24.   
  25.   // iniitalize frequencies for corner nodes
  26.   for (int i=0; i<nodes; i++){
  27.     frequency[i] = random(5, 12);
  28.   }
  29.   noStroke();
  30.   smooth();
  31.   frameRate(30);
  32. }

  33. void draw() {
  34.   
  35.   background(100);
  36.   drawShape();
  37.   moveShape();
  38.   
  39. }

  40. void drawShape() {  
  41.   float angle = 2*PI/nodes; 
  42.   
  43.   for(int i=0; i<nodes; i++) {
  44.     pointArray[i][0] = centerX + radius * sin(angle*i);
  45.     imgArray[i][0] = img.width/2 + (img.width/2) * sin(angle*i);    
  46.   }
  47.   
  48.   for(int i=0; i<nodes; i++) {
  49.     pointArray[i][1] = centerY+radius * cos(angle*i);
  50.     imgArray[i][1] = img.height/2 + (img.height/2) * cos(angle*i);
  51.     
  52.   }

  53.   // draw polygon
  54.   curveTightness(organicConstant);
  55.   fill(255);
  56.   beginShape(); 
  57.   texture(img);
  58.   for(int i=0; i<nodes; i++) {
  59.     vertex(pointArray[i][0], pointArray[i][1], 0, imgArray[i][0], imgArray[i][1]);
  60.   }
  61.   endShape(CLOSE);
  62. }

  63. void moveShape() {
  64.   //move center point
  65.   float deltaX = mouseX-centerX;
  66.   float deltaY = mouseY-centerY;

  67.   // create springing effect
  68.   deltaX *= springing;
  69.   deltaY *= springing;
  70.   accelX += deltaX;
  71.   accelY += deltaY;

  72.   // move predator's center
  73.   centerX += accelX;
  74.   centerY += accelY;

  75.   // slow down springing
  76.   accelX *= damping;
  77.   accelY *= damping;

  78.   // change curve tightness
  79.   organicConstant = 1 - ((abs(accelX) + abs(accelY)) * .1);
  80.   
  81.   for(int i=0; i< nodes; i++){
  82.     pointArray[i][0] = pointArray[i][0]+sin(radians(angles[i]))*(accelX*2); 
  83.     pointArray[i][1] = pointArray[i][1]+sin(radians(angles[i]))*(accelX*2);
  84.     angles[i]+=frequency[i];
  85.   }
  86.  
  87. }