Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example code to create an animation and image sequence
// Example code from http://futurefabric.co.uk/mooooooving
// Based on the work of Dave Whyte — http://beesandbombs.tumblr.com
int[][] result;
float t;
void setup() {
setup_();
result = new int[width*height][3];
}
void draw() {
if (!recording) {
t = mouseX*1.0/width;
draw_();
} else {
for (int i=0; i<width*height; i++)
for (int a=0; a<3; a++)
result[i][a] = 0;
for (int sa=0; sa<samplesPerFrame; sa++) {
t = map(frameCount-1 + sa*shutterAngle/samplesPerFrame, 0, numFrames, 0, 1);
draw_();
loadPixels();
for (int i=0; i<pixels.length; i++) {
result[i][0] += pixels[i] >> 16 & 0xff;
result[i][1] += pixels[i] >> 8 & 0xff;
result[i][2] += pixels[i] & 0xff;
}
}
loadPixels();
for (int i=0; i<pixels.length; i++)
pixels[i] = 0xff << 24 |
int(result[i][0]*1.0/samplesPerFrame) << 16 |
int(result[i][1]*1.0/samplesPerFrame) << 8 |
int(result[i][2]*1.0/samplesPerFrame);
updatePixels();
saveFrame("f###.png");
if (frameCount==numFrames)
exit();
}
}
//////////////////////////////////////////////////////////////////////////////
int samplesPerFrame = 1;
int numFrames = 100;
float shutterAngle = 0;
// PLAYBACK MODE
// Use the 'recording' variable to switch between preview and recording modes.
// false: when you run your sketch, you mouse position will control the animation.
// true: processing will save an image sequence of the animation to the same folder as this file
// (the number of files is based on numFrames above)
boolean recording = false;
class cuboid {
void draw(float x, float y, float w) {
// Front
beginShape(QUADS);
vertex(-x, -y, w);
vertex( x, -y, w);
vertex( x, y, w);
vertex(-x, y, w);
endShape();
// Back
beginShape(QUADS);
vertex( x, -y, -w);
vertex(-x, -y, -w);
vertex(-x, y, -w);
vertex( x, y, -w);
endShape();
// Bottom
beginShape(QUADS);
vertex(-x, y, w);
vertex( x, y, w);
vertex( x, y, -w);
vertex(-x, y, -w);
endShape();
// Top
beginShape(QUADS);
vertex(-x, -y, -w);
vertex( x, -y, -w);
vertex( x, -y, w);
vertex(-x, -y, w);
endShape();
// Right
beginShape(QUADS);
vertex( x, -y, w);
vertex( x, -y, -w);
vertex( x, y, -w);
vertex( x, y, w);
endShape();
// Left
beginShape(QUADS);
vertex(-x, -y, -w);
vertex(-x, -y, w);
vertex(-x, y, w);
vertex(-x, y, -w);
endShape();
}
}
void setup_() {
size(500, 500, P3D);
background(255);
ortho();
smooth(8);
noFill();
strokeWeight(2);
b = new cuboid();
}
cuboid b = new cuboid();
void draw_() {
translate(width/2,height/2);
background(255);
pushMatrix();
// t is the magic when previewing: it is a number between 0 and 1 based on where
// your cursor is across the sketch (mouseX)
rotateX(TWO_PI * t);
rotateY(-TWO_PI * t);
b.draw(100,100,100);
popMatrix();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment