topical media & game development
graphic-processing-learning-14-example-14-4-example-14-4.pde / pde
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com
// Example 14-4: Pyramid using beginShape(TRIANGLES)
void setup() {
size(200,200,P3D);
}
void draw() {
background(255);
// The pyramid's vertices are drawn relative to a centerpoint.
// Therefore, we call translate() to place the pyramid properly in the window.
// A slightly better option might be to include the translate in the drawPyramid() function and pass in x,y,z as arguments
translate(100,100,0);
drawPyramid(150);
}
// The function sets the vertices for the pyramid around the centerpoint at a flexible distance,
// depending on the number passed in as an argument.
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(255,150); // Note that each polygon can have its own color.
vertex(-t,-t,-t);
vertex( t,-t,-t);
vertex( 0, 0, t);
fill(150,150);
vertex( t,-t,-t);
vertex( t, t,-t);
vertex( 0, 0, t);
fill(255,150);
vertex( t, t,-t);
vertex(-t, t,-t);
vertex( 0, 0, t);
fill(150,150);
vertex(-t, t,-t);
vertex(-t,-t,-t);
vertex( 0, 0, t);
endShape();
}
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.