How Do I Apply a Texture To My 3D PShapes?

edited April 2018 in Library Questions

I currently have 2 issues:

  1. I can't correctly apply a texture image to my PShapes

  2. My trapezoid PShape does not fill properly

I currently have a "map.jpg" in the same folder as the processing file, so I do not think that is the issue. Any help would be greatly appreciated. Thank You.

    import peasy.*;
    import peasy.org.apache.commons.math.*;
    import peasy.org.apache.commons.math.geometry.*;

    PeasyCam cam;

      PVector p1 = new PVector(0, 0, 0);
      PVector p2 = new PVector(0, -100, 0);
      PVector p3 = new PVector(-100, -100, 0);
      PVector p4 = new PVector(-100, 0, 0);
      PVector p5 = new PVector(0, 0, -100); 
      PVector p6 = new PVector(0, -100, -100);
      PVector p7 = new PVector(-100, -100, -100);
      PVector p8 = new PVector(-100, 0, -100);
      PVector p9 = new PVector(-50,0,0);
      PVector p10 = new PVector(-100,-80,0);
      PVector p11 = new PVector(-100,-80,-100);
      PVector p12 = new PVector(-50,0,-100);

      PShape cube;
      PShape cylinder;
      PShape trapezoid;

      PImage img;

    void setup(){
      size(600,600, P3D);
      background(255);
      fill(50,205,50);
      img=loadImage("map.jpg");
      //noStroke();

      cam = new PeasyCam(this, 600);

      cylinder = createCylinder(40, 100, 300);
      cube = createShape();
      cube.beginShape(QUAD);
      texture(img);
      cube.vertex(p1.x, p1.y, p1.z);
      cube.vertex(p2.x, p2.y, p2.z);
      cube.vertex(p3.x, p3.y, p3.z);
      cube.vertex(p4.x, p4.y, p4.z);

      cube.vertex(p1.x, p1.y, p1.z);
      cube.vertex(p5.x, p5.y, p5.z);
      cube.vertex(p6.x, p6.y, p6.z);
      cube.vertex(p2.x, p2.y, p2.z);

      cube.vertex(p3.x, p3.y, p3.z);
      cube.vertex(p7.x, p7.y, p7.z);
      cube.vertex(p8.x, p8.y, p8.z);
      cube.vertex(p4.x, p4.y, p4.z);

      cube.vertex(p8.x, p8.y, p8.z);
      cube.vertex(p5.x, p5.y, p5.z);
      cube.vertex(p6.x, p6.y, p6.z);
      cube.vertex(p7.x, p7.y, p7.z);

      cube.vertex(p8.x, p8.y, p8.z);
      cube.vertex(p4.x, p4.y, p4.z);
      cube.vertex(p1.x, p1.y, p1.z);
      cube.vertex(p5.x, p5.y, p5.z);

      cube.vertex(p6.x, p6.y, p6.z);
      cube.vertex(p2.x, p2.y, p2.z);
      cube.vertex(p3.x, p3.y, p3.z);
      cube.vertex(p7.x, p7.y, p7.z);
      cube.endShape(CLOSE);

      trapezoid= createShape();
      trapezoid.beginShape();
      trapezoid.vertex(p1.x, p1.y, p1.z);
      trapezoid.vertex(p2.x, p2.y, p2.z);
      trapezoid.vertex(p3.x, p3.y, p3.z);
      trapezoid.vertex(p10.x, p10.y, p10.z);
      trapezoid.vertex(p9.x, p9.y, p9.z);

      trapezoid.vertex(p1.x, p1.y, p1.z);
      trapezoid.vertex(p5.x, p5.y, p5.z);
      trapezoid.vertex(p6.x, p6.y, p6.z);
      trapezoid.vertex(p2.x, p2.y, p2.z);

      trapezoid.vertex(p3.x, p3.y, p3.z);
      trapezoid.vertex(p7.x, p7.y, p7.z);
      trapezoid.vertex(p11.x, p11.y, p11.z);
      trapezoid.vertex(p10.x, p10.y, p10.z);

      trapezoid.vertex(p11.x, p11.y, p11.z);
      trapezoid.vertex(p12.x, p12.y, p12.z);
      trapezoid.vertex(p5.x, p5.y, p5.z);
      trapezoid.vertex(p6.x, p6.y, p6.z);
      trapezoid.vertex(p7.x, p7.y, p7.z);

      trapezoid.vertex(p11.x, p11.y, p11.z);
      trapezoid.vertex(p10.x, p10.y, p10.z);
      trapezoid.vertex(p9.x, p9.y, p9.z);
      trapezoid.vertex(p12.x, p12.y, p12.z);

      trapezoid.vertex(p9.x, p9.y, p9.z);
      trapezoid.vertex(p1.x, p1.y, p1.z);
      trapezoid.vertex(p5.x, p5.y, p5.z);

      trapezoid.vertex(p6.x, p6.y, p6.z);
      trapezoid.vertex(p7.x, p7.y, p7.z);
      trapezoid.vertex(p3.x, p3.y, p3.z);
      trapezoid.vertex(p2.x, p2.y, p2.z);



      trapezoid.endShape(CLOSE);
    }
    PShape createCylinder(int sides, float r, float h) {

      PShape cylinder = createShape(GROUP);
      float angle = 360 / sides;
      float halfHeight = h / 2;

      // draw top of the cylinder
      PShape top = createShape();
      top.beginShape();
      for (int i = 0; i < sides; i++) {
        float x = cos( radians( i * angle ) ) * r;
        float y = sin( radians( i * angle ) ) * r;
        top.vertex( x, y, -halfHeight);
      }
      top.endShape(CLOSE);
      cylinder.addChild(top); //adds top to heirarchy

      // draw bottom of the cylinder
      PShape bottom = createShape();
      bottom.beginShape();
      for (int i = 0; i < sides; i++) {
        float x = cos( radians( i * angle ) ) * r;
        float y = sin( radians( i * angle ) ) * r;
        bottom.vertex( x, y, halfHeight);
      }
      bottom.endShape(CLOSE);
      cylinder.addChild(bottom); //adds bottom to heirarchy

      // draw side of the cylinder
      PShape side = createShape();
      side.beginShape(QUAD_STRIP);
      for (int i = 0; i < sides + 1; i++) {
        float x = cos( radians( i * angle ) ) * r;
        float y = sin( radians( i * angle ) ) * r;
        side.vertex( x, y, halfHeight);
        side.vertex( x, y, -halfHeight);
      }
      side.endShape(CLOSE);
      cylinder.addChild(side); //adds side to heirarchy

      return cylinder;
    }
    void draw(){
      background(255); 
      rotateX(PI/3);
      rotateZ(PI/4);
      scale(.4);
      translate(-150,0,-100);

    //LeftFoot
      pushMatrix();
      translate(100,100,-200);
      scale(1,2,.5);
      shape(cube);    
      translate(-50,-70,-20);
      scale(.3,.15,.25);
      shape(cylinder);
      translate(0,275,0);
      shape(cylinder);
      popMatrix();

      //RightFoot
      pushMatrix();
      translate(0,100,-200);
      scale(1,2,.5);
      shape(cube);    
      translate(-50,-70,-20);
      scale(.3,.15,.25);
      shape(cylinder);
      translate(0,275,0);
      shape(cylinder);
      popMatrix();

      //RightAnkle
      pushMatrix();
      translate(0,100,-200);
      scale(1,2,.5);    
      translate(-50,-70,-10);
      scale(.32,.17,.25);
      shape(cylinder);
      popMatrix();

      //LeftAnkle
      pushMatrix();
      translate(100,100,-200);
      scale(1,2,.5);
      shape(cube);    
      translate(-50,-70,-10);
      scale(.32,.17,.25);
      shape(cylinder);
      popMatrix();

      //RightLeg
      pushMatrix();
      translate(0,100,-115);
      scale(1,2,1.5);    
      translate(-50,-70,-10);
      scale(.40,.20,.25);
      shape(cylinder);
      popMatrix();

      //LeftLeg
      pushMatrix();
      translate(100,100,-115);
      scale(1,2,1.5);    
      translate(-50,-70,-10);
      scale(.40,.20,.25);
      shape(cylinder);
      popMatrix();

      //Body
      pushMatrix();
      translate(100,25,25);
      scale(2,2,1);
      shape(cube);
      popMatrix();

      pushMatrix();
      translate(100,125,125);
      scale(2,2,1);
      shape(cube);
      popMatrix();

      pushMatrix();
      translate(100,125,125);
      rotate(29.85);
      scale(1,2,1);
      shape(cube);    
      popMatrix();

      pushMatrix();
      translate(100,-275,25);
      rotate(29.85);
      scale(1,2,.3);
      shape(cube);    
      popMatrix();

      pushMatrix();
      translate(100,-475,-5);
      rotate(29.85);
      scale(3,2,.3);
      shape(cube);    
      popMatrix();

      pushMatrix();
      translate(100,-775,25);
      rotate(29.85);
      scale(4,2,.3);
      shape(cube);    
      popMatrix();

      pushMatrix();
      translate(100,-560,35);
      scale(2,2,.4);
      translate(-68,-30,-20);
      scale(.15,.15,.25);
      shape(cylinder);
      translate(0,-275,0);
      shape(cylinder);
      translate(250,0,0);
      shape(cylinder);
      translate(0,275,0);
      shape(cylinder);
      popMatrix();

      pushMatrix();
      translate(100,-390,35);
      scale(2,2,.4);
      translate(-68,-30,-20);
      scale(.15,.15,.25);
      shape(cylinder);
      translate(0,-275,0);
      shape(cylinder);
      translate(250,0,0);
      shape(cylinder);
      translate(0,275,0);
      shape(cylinder);
      popMatrix();

      pushMatrix();
      translate(100,-182.5,10);
      scale(2,2,.4);
      translate(-68,-30,-20);
      scale(.15,.15,.25);
      translate(0,-275,0);
      shape(cylinder);
      translate(250,0,0);
      shape(cylinder);
      popMatrix();

      pushMatrix();
      translate(100,25,-75);
      rotateY(PI/2);
      rotateZ(-PI/2);
      scale(2,1,2);
      shape(trapezoid);    
      popMatrix();

      pushMatrix();
      translate(100,-75,125);
      rotateY(PI/2);
      rotateZ(PI/2);
      scale(2,1,2);
      shape(trapezoid);    
      popMatrix();

      pushMatrix();
      translate(100,25,225);
      rotateY(PI/2);
      rotateZ(PI/2);
      scale(2,1,2);
      shape(trapezoid);    
      popMatrix();

      pushMatrix();
      translate(-100,25,225);
      rotateY(PI/2);
      rotateZ(PI/2);
      rotateY(PI);
      scale(2,1,2);
      shape(trapezoid);    
      popMatrix();

      //Head
      pushMatrix();
      translate(100,-75,325);
      rotate(29.85);
      scale(1,2,1);
      shape(cube);    
      popMatrix();

      pushMatrix();
      translate(100,-75,355);
      rotate(29.85);
      scale(1,2,.3);
      shape(cube);    
      popMatrix();

      pushMatrix();
      translate(-100,-75,455);
      rotateY(PI/2);
      rotateZ(PI/2);
      rotateY(PI);
      scale(2,1,2);
      shape(trapezoid);    
      popMatrix();

      pushMatrix();
      translate(100,115,470);
      scale(2,2,.3);
      translate(-68,-30,-20);
      scale(.15,.15,.25);
      translate(0,-275,0);
      shape(cylinder);
      translate(250,0,0);
      shape(cylinder);
      popMatrix();

      pushMatrix();
      translate(100,25,255);
      rotate(29.85);
      scale(3,2,.3);
      shape(cube);  
      popMatrix();

      pushMatrix();
      translate(100,415,270);
      scale(2,2,.3);
      translate(-68,-30,-20);
      scale(.15,.15,.25);
      translate(0,-275,0);
      shape(cylinder);
      translate(250,0,0);
      shape(cylinder);
      popMatrix();

      pushMatrix();
      translate(100,25,285);
      rotate(29.85);
      scale(2,2,.3);
      shape(cube);    
      popMatrix();

      pushMatrix();
      translate(-100,25,355);
      rotateY(PI/2);
      rotateZ(PI/2);
      rotateY(PI);
      scale(2,.7,2);
      shape(trapezoid);    
      popMatrix();
    }

Answers

  • Line 38

    cube.texture(img);

    Kf

  • I made the change, but it just changed the shade of my green fill on the shape.

  • Answer ✓

    Try this:

      cube = createShape();
      cube.beginShape(QUAD);
      cube.textureMode(NORMAL);
      cube.texture(img);
      cube.vertex(p1.x, p1.y, p1.z,0,1);
      cube.vertex(p2.x, p2.y, p2.z,1,1);
      cube.vertex(p3.x, p3.y, p3.z,1,0);
      cube.vertex(p4.x, p4.y, p4.z,0,0);
    

    Kf

  • Thanks that helped!

Sign In or Register to comment.