Skip to content

Instantly share code, notes, and snippets.

Created September 1, 2014 18:02
Show Gist options
  • Save anonymous/4e1ad2bc0e83ffab9acf to your computer and use it in GitHub Desktop.
Save anonymous/4e1ad2bc0e83ffab9acf to your computer and use it in GitHub Desktop.
torus
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###.gif");
if (frameCount==numFrames)
exit();
}
}
//////////////////////////////////////////////////////////////////////////////
int samplesPerFrame = 32;
int numFrames = 72;
float shutterAngle = .4;
boolean recording = false;
void setup_() {
size(500, 500, P3D);
smooth(8);
rectMode(CENTER);
fill(64);
strokeWeight(1.2);
stroke(240);
}
float x, y, tt;
float th1 = 0, th2 = QUARTER_PI;
float R = 240, r1, r2;
int M = 40, N = 60;
float nx, ny, nv = 20;
void draw_() {
background(64);
pushMatrix();
translate(width/2, height/2 - 50, 50);
rotateX(-.85);
beginShape(TRIANGLE_STRIP);
for (int a=0; a<N; a ++) {
th1 = (a-2*t)*TWO_PI/N;
th2 = (a+1-2*t)*TWO_PI/N;
r1 = map(cos(TWO_PI*t - 3*th1), -1, 1, 145, 150);
r2 = map(cos(TWO_PI*t - 3*th2), -1, 1, 145, 150);
for (int i=0; i<M+1; i++) {
vertex((R+r1*cos(TWO_PI*(i+.5*(a%2))/M))*cos(th1), r1*sin(TWO_PI*(i+.5*(a%2))/M), (R+r1*cos(TWO_PI*(i+.5*(a%2))/M))*sin(th1));
vertex((R+r2*cos(TWO_PI*(i+.5*(a%2)+.5)/M))*cos(th2), r2*sin(TWO_PI*(i+.5*(a%2)+.5)/M), (R+r2*cos(TWO_PI*(i+.5*(a%2)+.5)/M))*sin(th2));
}
}
endShape();
popMatrix();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment