Happy Canada Day!

Hello folks!

Happy Canada Day!

/**
 * Textured 3D Cube with Canada Flag on Top Face
 * 
 * Author: glv
 * Date: 2025-07-01
 *
 * This sketch demonstrates drawing a 3D cube with a
 * texture applied only on the top face, without using
 * offscreen buffers, directly in Processing's P3D.
 */

PImage flag;

void setup() {
  size(300, 300, P3D);
  flag = loadImage("https://upload.wikimedia.org/wikipedia/commons/thumb/d/d9/Flag_of_Canada_%28Pantone%29.svg/500px-Flag_of_Canada_%28Pantone%29.svg.png");
  flag.resize(200, 100);
}

void draw() {
  background(10);
  lights();
  translate(width/2, height/2);

  // Rotate box for demo
  rotateX(radians(-60));
  rotateY(radians(frameCount));

  // Draw the box without fill or stroke
  noFill();
  stroke(255);
  box(80, 20, 180);

  // Draw textured top face manually as a quad
  float w = 40; // half width
  float d = 90; // half depth
  float y = -10; // top face

  beginShape(QUADS);
  texture(flag);

  // Rotate UV 90 degrees clockwise if needed:
  vertex(-w, y, -d, 0, flag.height);
  vertex( w, y, -d, 0, 0);
  vertex( w, y,  d, flag.width, 0);
  vertex(-w, y,  d, flag.width, flag.height);

  endShape();
}

References:

:)

4 Likes