Skip to content

Instantly share code, notes, and snippets.

@ipa
Created November 25, 2011 09:03
Show Gist options
  • Save ipa/1393091 to your computer and use it in GitHub Desktop.
Save ipa/1393091 to your computer and use it in GitHub Desktop.
3D Dice with Processing
Coordinate[] eyes = {
c(17, 17), c(17, 42), c(17, 67),
c(67, 17), c(67, 42), c(67, 67), c(42, 42)
};
int actualCube = 0;
void setup()
{
size (84, 84);
background(255);
smooth();
frameRate(1);
}
void draw()
{
actualCube += 1;
fill(0);
background(255);
switch(actualCube)
{
case 1:
ellipse(eyes[6].x, eyes[6].y, 10, 10);
break;
case 2:
ellipse(eyes[0].x, eyes[0].y, 10, 10);
ellipse(eyes[5].x, eyes[5].y, 10, 10);
break;
case 3:
ellipse(eyes[0].x, eyes[0].y, 10, 10);
ellipse(eyes[6].x, eyes[6].y, 10, 10);
ellipse(eyes[5].x, eyes[5].y, 10, 10);
break;
case 4:
ellipse(eyes[0].x, eyes[0].y, 10, 10);
ellipse(eyes[2].x, eyes[2].y, 10, 10);
ellipse(eyes[3].x, eyes[3].y, 10, 10);
ellipse(eyes[5].x, eyes[5].y, 10, 10);
break;
case 5:
ellipse(eyes[0].x, eyes[0].y, 10, 10);
ellipse(eyes[2].x, eyes[2].y, 10, 10);
ellipse(eyes[3].x, eyes[3].y, 10, 10);
ellipse(eyes[5].x, eyes[5].y, 10, 10);
ellipse(eyes[6].x, eyes[6].y, 10, 10);
break;
case 6:
ellipse(eyes[0].x, eyes[0].y, 10, 10);
ellipse(eyes[1].x, eyes[1].y, 10, 10);
ellipse(eyes[2].x, eyes[2].y, 10, 10);
ellipse(eyes[3].x, eyes[3].y, 10, 10);
ellipse(eyes[4].x, eyes[4].y, 10, 10);
ellipse(eyes[5].x, eyes[5].y, 10, 10);
break;
}
save("eyes"+actualCube+".gif");
actualCube %= 6;
}
Coordinate c(int x, int y)
{
return new Coordinate(x, y);
}
public class Coordinate
{
public int x;
public int y;
public Coordinate(int x, int y)
{
this.x = x;
this.y = y;
}
}
import processing.opengl.*;
PImage[] textures = new PImage[6];
float rotx = PI/4;
float roty = PI/4;
float transx = 0;
float transy = 0;
void setup()
{
for (int i = 1; i <= 6; i++)
{
textures[i-1] = loadImage("eyes"+i+".gif");
}
size(640, 480, OPENGL);
frameRate(18);
}
void draw()
{
background(0, 100);
directionalLight(204, 204, 204, 0, 0, -1);
translate(width/2 + transx, height/2 + transy, 0);
scale(100);
rotateX(rotx);
rotateY(roty);
drawCube();
}
void drawCube()
{
beginShape(QUAD);
// +Z "front" face
texture(textures[0]);
vertex(-1, -1, 1, 0, 84);
vertex( 1, -1, 1, 84, 84);
vertex( 1, 1, 1, 84, 0);
vertex(-1, 1, 1, 0, 0);
//vertex(-1, -1, 1);
endShape();
beginShape(QUAD);
// -Z "back" face
texture(textures[5]);
vertex( -1, -1, -1, 0, 84);
vertex(1, -1, -1, 84, 84);
vertex( 1, 1, -1, 84, 0);
vertex(-1, 1, -1, 0, 0);
endShape();
beginShape(QUAD);
texture(textures[1]);
// +Y "bottom" face
vertex(-1, 1, 1, 0, 84);
vertex( 1, 1, 1, 84, 84);
vertex( 1, 1, -1, 84, 0);
vertex(-1, 1, -1, 0, 0);
endShape();
beginShape(QUAD);
texture(textures[4]);
// -Y "top" face
vertex(-1, -1, -1, 0, 84);
vertex( 1, -1, -1, 84, 84);
vertex( 1, -1, 1, 84, 0);
vertex(-1, -1, 1, 0, 0);
endShape();
beginShape(QUAD);
texture(textures[2]);
// +X "right" face
vertex( 1, -1, 1, 0, 84);
vertex( 1, -1, -1, 84, 84);
vertex( 1, 1, -1, 84, 0);
vertex( 1, 1, 1, 0, 0);
endShape();
beginShape(QUAD);
texture(textures[3]);
// -X "left" face
vertex(-1, -1, -1, 0, 84);
vertex(-1, -1, 1, 84, 84);
vertex(-1, 1, 1, 84, 0);
vertex(-1, 1, -1, 0, 0);
endShape();
}
void mouseDragged()
{
float rate = 0.01;
rotx += (pmouseY-mouseY) * rate;
roty += (mouseX-pmouseX) * rate;
}
void keyPressed()
{
switch(keyCode)
{
case 38:
transy--;
break;
case 39:
transx++;
break;
case 37:
transx--;
break;
case 40:
transy++;
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment