import processing.core.*;
import SGCamera.*;
 
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.zip.*;
 
public class AntiAlias extends PApplet {
 
SGCamera cam;
int time = 0;
float dof = 1;
int iterations = 200;
Vector splats;
boolean running = false;
hdrColor[] buffer;
float jitX = -1 / (iterations / 2);
float jitY = -1 / (iterations / 2);
int dim = 50;
 
public void setup() {
size(550, 500, P3D);
frameRate(120);
cam = new SGCamera(this, 10, 10, 0, 0, 0, 0, SGCamera.ORBIT, SGCamera.DOLLY);
 
frameRate(30);
 
cam.lookAt(0, 0, 0);
cam.setPosition(30, -10, 0);
 
buffer = new hdrColor[width * height];
 
for (int i = 0; i < width * height; i++) {
buffer[i] = new hdrColor(0, 0, 0);
}
 
time = 0;
 
splats = buildStratified(1, 64);
 
}
 
public Vector buildStratified(float jitter, int samples) {
splats = new Vector();
int side = (int) sqrt(samples);
 
float pixWidth = 1.f / side;
 
 
for (float i = -.5f; i < .5f; i += 1.f / side) {
for (float j = -.5f; j < .5f; j += 1.f / side) {
splats.add(new PVector(i + random(-pixWidth, pixWidth), j + random(-pixWidth, pixWidth), 0));
 
}
}
return splats;
}
 
public void draw() {
 
background(32, 32, 32);
PVector sample = (PVector) splats.elementAt(time);
 
 
cam.dx = 10 * sample.x;
cam.dy = 10 * sample.y;
cam.feed();
 
 
 
pushMatrix();
rotateX(PI / 2);
 
int scale = 3;
 
fill(100, 200, 100, 200);
for (int i = -20; i < 20; i++) {
for (int j = -20; j < 20; j++) {
if ((i + j) % 2 == 0) {
fill(0);
}
else {
fill(255);
}
rect(scale * i, scale * j, scale, scale);
}
}
popMatrix();
 
lights();
noStroke();
fill(50, 50, 150);
translate(0, -10, 0);
rotateX(PI / 4);
rotateY(PI / 4);
box(10);
 
if (running && time < splats.size() - 1) {
time++;
loadPixels();
for (int i = 0; i < width * height; i++) {
 
if (i > 0) {
buffer[i - 1].add(pixels[i], .5f);
}
buffer[i].add(pixels[i], 1);
 
}
 
}
else if (running) {
for (int i = 0; i < width * height; i++) {
pixels[i] = buffer[i].toLDR(iterations);
}
updatePixels();
 
}
 
}
 
public void mousePressed() {
time = 0;
running = false;
 
for (int i = 0; i < width * height; i++) {
buffer[i] = new hdrColor(0, 0, 0);
}
 
}
 
public void keyPressed() {
running = true;
 
}
 
public class hdrColor {
 
float r, g, b, a;
float weight;
 
public hdrColor(float r, float g, float b) {
this.r = r;
this.g = g;
this.b = b;
}
 
public void add(int c1, float weight) {
r += weight * (c1 >> 16 & 0xff);
g += weight * (c1 >> 8 & 0xff);
b += weight * (c1 & 0xff);
this.weight += weight;
}
 
public int toLDR(float exposure) {
return color(r / weight, g / weight, b / weight);
 
}
}
 
static public void main(String args[]) {
PApplet.main(new String[]{"AntiAlias"});
}
}