Cameras
Lenses
By default, P5Sunflow is going to use a pinhole camera for your scene. This is generally the quickest way to render, but fancier options exist. I'll use the following as our code base:
// Set up the scene, make sure we don't loop
void setup() {
size(510, 300, "hipstersinc.P5Sunflow");
noStroke();
noLoop();
}
// Draw our scene
void draw() {
setupCamera();
background(255);
// Adjust perspective
translate(width/2, height/2, 100);
rotateX(QUARTER_PI);
rotateZ(QUARTER_PI);
translate(-width/2, -height/2);
// Draw some boxes
float boxSize = width / 11;
translate(boxSize, boxSize);
for(int i=0; i<3; i++) {
for(int j=0; j<5; j++) {
pushMatrix();
translate(j*boxSize, j*boxSize);
fill(j * 50);
box(boxSize);
popMatrix();
}
translate(boxSize * 2, 0);
}
}
// Tweak our camera parameters. Make sure to call this from `draw`
void setupCamera() {
// Get the P5Sunflow PGraphics
P5Sunflow sunflow = (P5Sunflow) g;
// Set our camera to "pinhole"
sunflow.camera.setType(SunflowCamera.PINHOLE);
}
Copy/PasteYou can see we're setting the camera lens type by and in the setupCamera() method. Below is the output of the various types of lenses available.
Pinhole
void setupCamera() {
P5Sunflow sunflow = (P5Sunflow) g;
sunflow.camera.setType(SunflowCamera.PINHOLE);
}
Copy/Paste
Pretty basic lens (or lack thereof). This will do the trick in 90% of cases. It's also considerably faster than your other options. If you don't need depth of field, or crazy viewing angles, this is a good bet.
Thinlens
void setupCamera() {
P5Sunflow sunflow = (P5Sunflow) g;
sunflow.camera.setType(SunflowCamera.THINLENS);
}
Copy/Paste
While the pinhole lens will trace all rays through a point, the thinlens actually models an area for the light to pass through. You'll notice the image looks pretty damn similar with the default settings. We'll get in to some tweaks further down the page.
Spherical
void setupCamera() {
P5Sunflow sunflow = (P5Sunflow) g;
sunflow.camera.setType(SunflowCamera.SPHERICAL);
}
Copy/Paste
The spherical lens will render your entire environment. Take care to note that if you're using this lens in your project for the purpose of displaying a sketch in a planetarium, you had better call my sorry ass up. That's something everyone needs to see.
Fisheye
void setupCamera() {
P5Sunflow sunflow = (P5Sunflow) g;
sunflow.camera.setType(SunflowCamera.FISHEYE);
}
Copy/Paste
Kind of what you'd expect from a lens called "fisheye," I guess.
Depth of Field
There are a few properties of the thinlens that let us emulate photographic effects. Depth of field is one of simpler ones. If you own a manual camera, you know that one of the things that affects the depth of field is our lens size.
void setupCamera() {
P5Sunflow sunflow = (P5Sunflow) g;
// Make sure we have a thinlens
sunflow.camera.setType(SunflowCamera.THINLENS);
// Focus it somewhere around the center of the scene. This
// often just has to be tweaked by hand to your liking.
sunflow.camera.setFocalDistance(450);
// Put a huge lens on the camera. This too will often have
// to be tweaked by hand.
sunflow.camera.setLensRadius(12f);
}
Copy/Paste
Wow. That looks like shit. That looks like shit because we're not taking enough samples per pixel. Sunflow is having a hard time figuring out which rays are bouncing around in the blurry parts. I'll cover this a bit more in depth in the scene settings section, but here's the long and short of it...
void setupCamera() {
P5Sunflow sunflow = (P5Sunflow) g;
sunflow.camera.setType(SunflowCamera.THINLENS);
sunflow.camera.setFocalDistance(450);
sunflow.camera.setLensRadius(12f);
// Now we set the Anti-Aliasing sampling.
sunflow.scene.setAaSamples(12);
}
Copy/Paste
Much better. We're telling Sunflow to render every 1/12th of a pixel. It ends up just averaging those values together when everything is in focus, but it really helps us figure out what's going on between the pixels when things are out of focus.
You can see the closest cubes are still a little grainy, but I'm dealing with a ridiculously large lens to better illustrate. However, if you encounter this problem, you can keep raising the samples until it disappears. Keep in mind that rendering time quadruples for every doubling of this number (I think). You can sink some real time into setting this too high.
Other Options
setFieldofView(float f)
How many degrees the camera can see. By default, this is set by some odd-looking algorithm yanked straight from processing. It seems to do the trick.
setAspectRatio(float f)
A width/height ratio. By default, it is the ratio of your applet.
