Daymnn
Daymnn

Reputation: 229

Making a shape in processing

I was working on an animation on processing. Then, I have a question about the code. Normally, my code is more long. However, I made a simple code which can usefull also for the beginners. My sample code:

 void setup() {
  size(250, 250);
}

void draw() {
  background(102);
  translate(100, 100);
  beginShape();
  vertex(0,0);
  vertex(-50, 50);
  vertex(0, 100);
  endShape();
}

I have to use three vertexes and I want to texture the Shape as in texture(). How to make that?

Upvotes: 0

Views: 535

Answers (1)

Laos Los
Laos Los

Reputation: 176

In order to put an image as a texture on a Shape:

  1. Use a P2D or a P3D render, instead of the default render.
  2. Load the image which is going to texture your Shape.
  3. Call the texture() function when you begin to set up the Shape.
  4. Establish the mode (IMAGE) and the wrap (CLAMP) for the texture image.
  5. Choose three points of the image as the vertices for the triangle. For example (0, 0), (0, img.height) and (img.width, img.height).
PImage img;

void setup() {
  size(250, 250, P2D);
  img = loadImage("rectangular_image.jpg");
  textureMode(IMAGE);
  textureWrap(CLAMP);
}

void draw() {
  background(102);
  translate(100, 100);
  noStroke();
  beginShape();
  texture(img);
  vertex(0, 0, 0, 0);
  vertex(-50, 50, 0, img.height);
  vertex(0, 100, img.width, img.height);
  endShape();
}

Upvotes: 1

Related Questions