This example is for Processing version 1.0+. If you have a previous version, use the examples included with your software. If you see any errors or have comments, please let us know.
Brick Tower by Ira Greenberg.
3D castle tower constructed out of individual bricks. Uses the PVector and Cube classes.
float bricksPerLayer = 16.0;
float brickLayers = 18.0;
Cube brick;
float brickWidth = 60, brickHeight = 25, brickDepth = 25;
float radius = 175.0;
float angle = 0;
void setup(){
size(640, 360, P3D);
brick = new Cube(brickWidth, brickHeight, brickDepth);
}
void draw(){
background(0);
float tempX = 0, tempY = 0, tempZ = 0;
fill(182, 62, 29);
noStroke();
// Add basic light setup
lights();
translate(width/2, height*1.2, -380);
// Tip tower to see inside
rotateX(radians(-45));
// Slowly rotate tower
rotateY(frameCount * PI/600);
for (int i = 0; i < brickLayers; i++){
// Increment rows
tempY-=brickHeight;
// Alternate brick seams
angle = 360.0 / bricksPerLayer * i/2;
for (int j = 0; j < bricksPerLayer; j++){
tempZ = cos(radians(angle))*radius;
tempX = sin(radians(angle))*radius;
pushMatrix();
translate(tempX, tempY, tempZ);
rotateY(radians(angle));
// Add crenelation
if (i==brickLayers-1){
if (j%2 == 0){
brick.create();
}
}
// Create main tower
else {
brick.create();
}
popMatrix();
angle += 360.0/bricksPerLayer;
}
}
}
class Cube {
PVector[] vertices = new PVector[24];
float w, h, d;
Cube(){ }
Cube(float w, float h, float d){
this.w = w;
this.h = h;
this.d = d;
// Cube composed of 6 quads
// Front
vertices[0] = new PVector(-w/2, -h/2, d/2);
vertices[1] = new PVector(w/2, -h/2, d/2);
vertices[2] = new PVector(w/2, h/2, d/2);
vertices[3] = new PVector(-w/2, h/2, d/2);
// Left
vertices[4] = new PVector(-w/2, -h/2, d/2);
vertices[5] = new PVector(-w/2, -h/2, -d/2);
vertices[6] = new PVector(-w/2, h/2, -d/2);
vertices[7] = new PVector(-w/2, h/2, d/2);
// Right
vertices[8] = new PVector(w/2, -h/2, d/2);
vertices[9] = new PVector(w/2, -h/2, -d/2);
vertices[10] = new PVector(w/2, h/2, -d/2);
vertices[11] = new PVector(w/2, h/2, d/2);
// Back
vertices[12] = new PVector(-w/2, -h/2, -d/2);
vertices[13] = new PVector(w/2, -h/2, -d/2);
vertices[14] = new PVector(w/2, h/2, -d/2);
vertices[15] = new PVector(-w/2, h/2, -d/2);
// Top
vertices[16] = new PVector(-w/2, -h/2, d/2);
vertices[17] = new PVector(-w/2, -h/2, -d/2);
vertices[18] = new PVector(w/2, -h/2, -d/2);
vertices[19] = new PVector(w/2, -h/2, d/2);
// Bottom
vertices[20] = new PVector(-w/2, h/2, d/2);
vertices[21] = new PVector(-w/2, h/2, -d/2);
vertices[22] = new PVector(w/2, h/2, -d/2);
vertices[23] = new PVector(w/2, h/2, d/2);
}
void create(){
for (int i=0; i<6; i++){
beginShape(QUADS);
for (int j = 0; j < 4; j++){
vertex(vertices[j+4*i].x, vertices[j+4*i].y, vertices[j+4*i].z);
}
endShape();
}
}
}


