Para o primeiro exercício procurei explorar o 3D para obter uma vista de um figura geométrica, o cubo.
void setup() { size(500, 500, P3D); noFill(); smooth(); background(#5EAFAA); } void draw() { float fov = PI/3.10; noFill(); stroke(255); float cameraZ = (height/10.0) / tan(fov/10.0); perspective(fov, float(width)/float(height), cameraZ/10.0, cameraZ*15.0); //coordenadas do cubo translate(250, 230, 0); //rotação rotateX(-PI/4.5); rotateY(PI/5); //tamanho do cubo box(250); }
Na sequência desta experiência decidi explorar os códigos que me permitissem não só animar uma figura geométrica como criar algum tipo de interactividade com outras. Dai surgiram várias experiências das quais se destaca a seguinte:
float r; float g; float b; float backgroundMousea; float backgroundMouseb; float backgroundMousec=67; float locX; float locY; // 10000 pequenos cubos Cube[] cubies = new Cube[10000]; // Cubo float bounds = 300; void setup() { size(500, 500, P3D); for (int i = 0; i < cubies.length; i++) { // Cubies are randomly sized float cubieSize = random(5, 15); cubies[i] = new Cube(cubieSize, cubieSize, cubieSize); } } void draw() { //variaveis r=random(255); g=random(255); b=random(255); //fundo background(#5EAFAA); lights(); // definir a tranlação central translate(width/2, height/2, -130); // rodar tudo inclusive o cubo grande rotateX(frameCount * 0.001); rotateY(frameCount * 0.002); rotateZ(frameCount * 0.001); stroke(255); // Usar box para o cubo transparente noFill(); box(bounds); // Mover e rodar Cubos for (Cube c : cubies) { c.update(); c.display(); } } // Criação do Cubo transparente class Cube { // Posição e velocidade dos vectores PVector position; PVector velocity; //Usar PVector para permitir a rotação PVector rotation; // Vertices do Cubo PVector[] vertices = new PVector[24]; // width, height, depth float w, h, d; // Cores das faces dos cubos (6 faces) color[] quadBG = new color[6]; Cube(float w, float h, float d) { this.w = w; this.h = h; this.d = d; // Cores das 6 faces dos cubos quadBG[0] = color(#FFFFFF); quadBG[1] = color(#5EAFAA); quadBG[2] = color(#5EAFAA); quadBG[3] = color(#FFFFFF); quadBG[4] = color(#000000); quadBG[5] = color(#000000); // Começar no centro position = new PVector(); // Velocidade aleatoria velocity = PVector.random3D(); // Rotação aleatoria rotation = new PVector(random(40, 100), random(40, 100), random(40, 100)); // 6 quadrantes do cubo //front vertices[0] = new PVector(-w/10, -h/10, d/10); vertices[1] = new PVector(w/10, -h/10, d/10); vertices[2] = new PVector(w/10, h/10, d/10); vertices[3] = new PVector(-w/10, h/10, d/10); //left vertices[4] = new PVector(-w/10, -h/10, d/10); vertices[5] = new PVector(-w/10, -h/10, -d/10); vertices[6] = new PVector(-w/10, h/10, -d/10); vertices[7] = new PVector(-w/10, h/10, d/10); //right vertices[8] = new PVector(w/10, -h/10, d/10); vertices[9] = new PVector(w/10, -h/10, -d/10); vertices[10] = new PVector(w/10, h/10, -d/10); vertices[11] = new PVector(w/10, h/10, d/10); //back vertices[12] = new PVector(-w/10, -h/10, -d/10); vertices[13] = new PVector(w/10, -h/10, -d/10); vertices[14] = new PVector(w/10, h/10, -d/10); vertices[15] = new PVector(-w/10, h/10, -d/10); //top vertices[16] = new PVector(-w/10, -h/10, d/10); vertices[17] = new PVector(-w/10, -h/10, -d/10); vertices[18] = new PVector(w/10, -h/10, -d/10); vertices[19] = new PVector(w/10, -h/10, d/10); //bottom vertices[20] = new PVector(-w/10, h/10, d/10); vertices[21] = new PVector(-w/10, h/10, -d/10); vertices[22] = new PVector(w/10, h/10, -d/10); vertices[23] = new PVector(w/10, h/10, d/10); } void drawCube() { // desenho do cubo for (int i=0; i<6; i++) { fill(quadBG[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(); } } void display() { pushMatrix(); translate(position.x, position.y, position.z); rotateX(frameCount*PI/rotation.x); rotateY(frameCount*PI/rotation.y); rotateZ(frameCount*PI/rotation.z); noStroke(); drawCube(); // outro método popMatrix(); } }