

import processing.video.*;
Capture video;
void setup()
{
int w = 800;
size(int(PI*(w/2.0)), w/2, P2D);
video = new Capture(this, w, w, 12);
}
void draw()
{
if (video.available())
{
video.read();
video.loadPixels();
loadPixels();
for( int y=0; y<height; y++ )
{
for( int x=0; x<width; x++ )
{
float theta = map(x, 0, width, 0, TWO_PI);
float radius = map( (height-1-y), 0, height, 0, video.width/2.0);
//float radius = sin( (y/float(height))*HALF_PI ) * video.width/2.0;
int imgX = int( cos(theta) * radius + video.width/2.0 );
int imgY = int( sin(theta) * radius + video.height/2.0 );
pixels[ y*width + x ] = video.pixels[ imgY*video.width + imgX ];
}
}
updatePixels();
}
}
void keyPressed()
{
if (key==' ')
{
saveFrame("polar###.png");
}
}
Whipped this up quickly for a question on the Processing forum, and it turned out to be fun. I have to remember to dig it out again next time the nephews visit…
Tuesday, January 27, 2009
Processing hack: You can fake nice shading on flat polys by generating a simple shadow map and texturing it on. On complex images this looks pretty good.
Here’s a simple example:

Summary:
- Render a white ellipse to a separate PGraphics object
- Blur it out
- Pre-calculate uv coordinates
- Use tint() and texture() to use the shadow map (only works in P3D?)
- Pass the uvs to the vertex() calls for a shape
Code:
void setup()
{
size( 400, 400, P3D );
smooth();
background(0);
noStroke();
// create a texture of a white shape on a black background
float shadingSize = 64.0;
PGraphics shading = createGraphics( (int)shadingSize, (int)((shadingSize/2.0)*tan(PI/3.0)), JAVA2D );
shading.beginDraw();
shading.background(0);
shading.smooth();
shading.fill(255);
float radius = (shadingSize/2.0)*tan(PI/6.0);
shading.ellipse( shading.width/2.0, shading.height-radius, radius, radius*2.5 );
shading.filter(BLUR, 10.0);
shading.endDraw();
// maps the uv coordinates
float[][] uv = new float[3][2];
uv[0][0] = shading.width/2.0;
uv[0][1] = 0.0f;
uv[1][0] = shading.width;
uv[1][1] = shading.height;
uv[2][0] = 0.0f;
uv[2][1] = shading.height;
translate( width/2, height/2 );
int numSides = 6;
// draw a wheel, texturing each poly
beginShape(TRIANGLE_FAN);
texture( shading );
tint(0);
vertex( 0, 0, uv[0][0], uv[0][1] );
for (int i=0; i< numSides+1; i++ )
{
tint( abs(cos( i * PI/numSides )) * 255, 0, abs(sin( i * PI/numSides )) * 255 );
vertex( cos( i * TWO_PI/numSides ) * width/2, sin( i * TWO_PI/numSides ) * width/2, uv[i%2+1][0], uv[i%2+1][1] );
}
endShape(CLOSE);
}
(not sure how to get a login for the real Processing Hacks page…)