Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 14-10: Pyramid

float theta = 0.0;

void setup() {
  size(200,200,P3D);
}

void draw() {
  background(255);
  theta += 0.01;
  
  translate(100,100,0);
  rotateX(theta);
  rotateY(theta);
  drawPyramid(50);
  
  // translate the scene again
  translate(50,50,20);
  // call the pyramid drawing function
  drawPyramid(10);
}

void drawPyramid(int t) {
  stroke(0);
  
  // this pyramid has 4 sides, each drawn as a separate triangle
  // each side has 3 vertices, making up a triangle shape
  // the parameter " t " determines the size of the pyramid
  beginShape(TRIANGLES);
  
  fill(150,0,0,127);
  vertex(-t,-t,-t);
  vertex( t,-t,-t);
  vertex( 0, 0, t);
  
  fill(0,150,0,127);
  vertex( t,-t,-t);
  vertex( t, t,-t);
  vertex( 0, 0, t);
  
  fill(0,0,150,127);
  vertex( t, t,-t);
  vertex(-t, t,-t);
  vertex( 0, 0, t);
  
  fill(150,0,150,127);
  vertex(-t, t,-t);
  vertex(-t,-t,-t);
  vertex( 0, 0, t);
  
  endShape();
}
  • Walter

    Hello!
    A very fine effect with the rotating pyramides is reached, when you couple the colors and transparency to the angle.
    e.g:
    c=color(100*alpha%255,100*alpha%255,0 ,100*alpha%100);
    fill(c);
    vertex(t,-t,-t );
    vertex(t,t,-t );
    vertex(0,0,t );

  • Yahya

    That truly is a fine effect! Using transparency like this simulates the physical use of stained glass.

  • Walter

    Bolschoj National Ballet:
    void drawPyramid(int t)
    {
    stroke(20);
    alpha=alpha+0.005;
    translate (t ,t ,0);
    for (int i=0;i<10;i++) //new
    {
    rotateY(alpha*t/100);
    rotateX(alpha*t/101);
    rotateZ(alpha*t/50);
    beginShape(TRIANGLES);
    c=color(255 ,0,0 ,100 );
    fill(c);
    vertex(-t,-t,-t );
    vertex(t,-t,-t );
    vertex(0,0,t );

    c=color( 255, 255,0 , 100);
    fill(c);
    vertex(t,-t,-t );
    vertex(t,t,-t );
    vertex(0,0,t );

    c=color(255,100*alpha%255, 255 ,100);
    fill(c);
    vertex(t,t,-t );
    vertex(-t,t,-t );
    vertex(0,0,t);

    c=color(255%alpha%255,0,255*alpha%255 ,100);
    fill(c);
    vertex(-t,t,-t );
    vertex(-t,-t,-t );
    vertex(0,0,t);
    endShape(CLOSE);
    }
    }