moving the OpenGL examples to the core

This commit is contained in:
benfry
2012-07-20 20:24:58 +00:00
parent b30328bb66
commit 40ecd562e3
133 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
class Particle {
PVector velocity;
float lifespan = 255;
PShape part;
float partSize;
PVector gravity = new PVector(0,0.1);
Particle() {
partSize = random(10,60);
part = createShape(QUAD);
part.noStroke();
part.texture(sprite);
part.normal(0, 0, 1);
part.vertex(-partSize/2, -partSize/2, 0, 0);
part.vertex(+partSize/2, -partSize/2, sprite.width, 0);
part.vertex(+partSize/2, +partSize/2, sprite.width, sprite.height);
part.vertex(-partSize/2, +partSize/2, 0, sprite.height);
part.end();
rebirth(width/2,height/2);
lifespan = random(255);
}
PShape getShape() {
return part;
}
void rebirth(float x, float y) {
float a = random(TWO_PI);
float speed = random(0.5,4);
velocity = new PVector(cos(a), sin(a));
velocity.mult(speed);
lifespan = 255;
part.resetMatrix();
part.translate(x, y);
}
boolean isDead() {
if (lifespan < 0) {
return true;
} else {
return false;
}
}
public void update() {
lifespan = lifespan - 1;
velocity.add(gravity);
part.tint(255,lifespan);
part.translate(velocity.x, velocity.y);
}
}

View File

@@ -0,0 +1,36 @@
class ParticleSystem {
ArrayList<Particle> particles;
PShape particleShape;
ParticleSystem(int n) {
particles = new ArrayList<Particle>();
particleShape = createShape(PShape.GROUP);
for (int i = 0; i < n; i++) {
Particle p = new Particle();
particles.add(p);
particleShape.addChild(p.getShape());
}
}
void update() {
for (Particle p : particles) {
p.update();
}
}
void setEmitter(float x, float y) {
for (Particle p : particles) {
if (p.isDead()) {
p.rebirth(x, y);
}
}
}
void display() {
shape(particleShape);
}
}

View File

@@ -0,0 +1,31 @@
// Particles, by Daniel Shiffman
ParticleSystem ps;
PImage sprite;
void setup() {
size(640, 400, P3D);
orientation(LANDSCAPE);
sprite = loadImage("sprite.png");
ps = new ParticleSystem(10000);
// Writing to the depth buffer is disabled to avoid rendering
// artifacts due to the fact that the particles are semi-transparent
// but not z-sorted.
hint(DISABLE_DEPTH_MASK);
}
void draw () {
background(0);
ps.update();
ps.display();
ps.setEmitter(mouseX,mouseY);
fill(255);
textSize(16);
text("Frame rate: " + int(frameRate),10,20);
}

View File

@@ -0,0 +1,161 @@
// Bezier patch By Maritus Watz:
// http://www.openprocessing.org/sketch/57709
// Normal calculation added by Andres Colubri
// Direct port of sample code by Paul Bourke.
// Original code: http://paulbourke.net/geometry/bezier/
int ni=4, nj=5, RESI=ni*10, RESJ=nj*10;
PVector outp[][], inp[][];
PVector normp[][];
boolean autoNormals = false;
void setup() {
size(600, 600, P3D);
build();
}
void draw() {
background(255);
translate(width/2,height/2);
lights();
scale(0.9);
rotateY(map(mouseX,0,width,-PI,PI));
rotateX(map(mouseY,0,height,-PI,PI));
noStroke();
fill(255);
for(int i=0; i<RESI-1; i++) {
beginShape(QUAD_STRIP);
for(int j=0; j<RESJ; j++) {
if (!autoNormals) {
normal(normp[i][j].x, normp[i][j].y, normp[i][j].z);
}
vertex(outp[i][j].x,outp[i][j].y,outp[i][j].z);
vertex(outp[i+1][j].x,outp[i+1][j].y,outp[i+1][j].z);
}
endShape();
}
}
void keyPressed() {
if(key==' ') build();
saveFrame("bezPatch.png");
}
void build() {
int i, j, ki, kj;
double mui, muj, bi, bj, dbi, dbj;
outp=new PVector[RESI][RESJ];
normp=new PVector[RESI][RESJ];
inp=new PVector[ni+1][nj+1];
PVector uitang = new PVector();
PVector ujtang = new PVector();
for (i=0;i<=ni;i++) {
for (j=0;j<=nj;j++) {
inp[i][j]=new PVector(i,j,random(-3,3));
}
}
for (i=0;i<RESI;i++) {
mui = i / (double)(RESI-1);
for (j=0;j<RESJ;j++) {
muj = j / (double)(RESJ-1);
outp[i][j]=new PVector();
uitang.set(0, 0, 0);
ujtang.set(0, 0, 0);
for (ki=0;ki<=ni;ki++) {
bi = BezierBlend(ki, mui, ni);
dbi = DBezierBlend(ki, mui, ni);
for (kj=0;kj<=nj;kj++) {
bj = BezierBlend(kj, muj, nj);
dbj = DBezierBlend(kj, muj, nj);
outp[i][j].x += (inp[ki][kj].x * bi * bj);
outp[i][j].y += (inp[ki][kj].y * bi * bj);
outp[i][j].z += (inp[ki][kj].z * bi * bj);
uitang.x += (inp[ki][kj].x * dbi * bj);
uitang.y += (inp[ki][kj].y * dbi * bj);
uitang.z += (inp[ki][kj].z * dbi * bj);
ujtang.x += (inp[ki][kj].x * bi * dbj);
ujtang.y += (inp[ki][kj].y * bi * dbj);
ujtang.z += (inp[ki][kj].z * bi * dbj);
}
}
outp[i][j].add(new PVector(-ni/2,-nj/2,0));
outp[i][j].mult(100);
uitang.normalize();
ujtang.normalize();
normp[i][j] = uitang.cross(ujtang);
}
}
}
double BezierBlend(int k, double mu, int n) {
int nn, kn, nkn;
double blend=1;
nn = n;
kn = k;
nkn = n - k;
while (nn >= 1) {
blend *= nn;
nn--;
if (kn > 1) {
blend /= (double)kn;
kn--;
}
if (nkn > 1) {
blend /= (double)nkn;
nkn--;
}
}
if (k > 0)
blend *= Math.pow(mu, (double)k);
if (n-k > 0)
blend *= Math.pow(1-mu, (double)(n-k));
return(blend);
}
double DBezierBlend(int k, double mu, int n) {
int nn, kn, nkn;
double dblendf = 1;
nn = n;
kn = k;
nkn = n - k;
while (nn >= 1) {
dblendf *= nn;
nn--;
if (kn > 1) {
dblendf /= (double)kn;
kn--;
}
if (nkn > 1) {
dblendf /= (double)nkn;
nkn--;
}
}
double fk = 1;
double dk = 0;
double fnk = 1;
double dnk = 0;
if (k > 0) {
fk = Math.pow(mu, (double)k);
dk = k*Math.pow(mu, (double)k-1);
}
if (n-k > 0) {
fnk = Math.pow(1-mu, (double)(n-k));
dnk = (k-n)*Math.pow(1-mu, (double)(n-k-1));
}
dblendf *= (dk * fnk + fk * dnk);
return(dblendf);
}

View File

@@ -0,0 +1,261 @@
// Implementation of 1D, 2D, and 3D Perlin noise. Based on the
// C code by Paul Bourke:
// http://local.wasp.uwa.edu.au/~pbourke/texture_colour/perlin/
class Perlin {
int B = 0x100;
int BM = 0xff;
int N = 0x1000;
int NP = 12;
int NM = 0xfff;
int p[];
float g3[][];
float g2[][];
float g1[];
void normalize2(float v[]) {
float s = sqrt(v[0] * v[0] + v[1] * v[1]);
v[0] = v[0] / s;
v[1] = v[1] / s;
}
void normalize3(float v[]) {
float s = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
v[0] = v[0] / s;
v[1] = v[1] / s;
v[2] = v[2] / s;
}
float sCurve(float t) {
return t * t * (3.0 - 2.0 * t);
}
float at2(float q[], float rx, float ry) {
return rx * q[0] + ry * q[1];
}
float at3(float q[], float rx, float ry, float rz) {
return rx * q[0] + ry * q[1] + rz * q[2];
}
Perlin() {
p = new int[B + B + 2];
g3 = new float[B + B + 2][3];
g2 = new float[B + B + 2][2];
g1 = new float[B + B + 2];
init();
}
void init() {
int i, j, k;
for (i = 0 ; i < B ; i++) {
p[i] = i;
g1[i] = (random(B + B) - B) / B;
for (j = 0 ; j < 2 ; j++)
g2[i][j] = (random(B + B) - B) / B;
normalize2(g2[i]);
for (j = 0 ; j < 3 ; j++)
g3[i][j] = (random(B + B) - B) / B;
normalize3(g3[i]);
}
while (0 < --i) {
k = p[i];
p[i] = p[j = int(random(B))];
p[j] = k;
}
for (i = 0 ; i < B + 2 ; i++) {
p[B + i] = p[i];
g1[B + i] = g1[i];
for (j = 0 ; j < 2 ; j++)
g2[B + i][j] = g2[i][j];
for (j = 0 ; j < 3 ; j++)
g3[B + i][j] = g3[i][j];
}
}
float noise1(float[] vec) {
int bx0, bx1;
float rx0, rx1, sx, t, u, v;
t = vec[0] + N;
bx0 = int(t) & BM;
bx1 = (bx0 + 1) & BM;
rx0 = t - int(t);
rx1 = rx0 - 1.0;
sx = sCurve(rx0);
u = rx0 * g1[p[bx0]];
v = rx1 * g1[p[bx1]];
return lerp(u, v, sx);
}
float noise2(float[] vec) {
int bx0, bx1, by0, by1, b00, b10, b01, b11;
float rx0, rx1, ry0, ry1, sx, sy, a, b, t, u, v;
float[] q;
int i, j;
t = vec[0] + N;
bx0 = int(t) & BM;
bx1 = (bx0 + 1) & BM;
rx0 = t - int(t);
rx1 = rx0 - 1.0;
t = vec[1] + N;
by0 = int(t) & BM;
by1 = (by0 + 1) & BM;
ry0 = t - int(t);
ry1 = ry0 - 1.0;
i = p[bx0];
j = p[bx1];
b00 = p[i + by0];
b10 = p[j + by0];
b01 = p[i + by1];
b11 = p[j + by1];
sx = sCurve(rx0);
sy = sCurve(ry0);
q = g2[b00];
u = at2(q, rx0, ry0);
q = g2[b10];
v = at2(q, rx1, ry0);
a = lerp(u, v, sx);
q = g2[b01] ;
u = at2(q, rx0, ry1);
q = g2[b11] ;
v = at2(q, rx1, ry1);
b = lerp(u, v, sx);
return lerp(a, b, sy);
}
float noise3(float[] vec) {
int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
float rx0, rx1, ry0, ry1, rz0, rz1, sy, sz, a, b, c, d, t, u, v;
float[] q;
int i, j;
t = vec[0] + N;
bx0 = int(t) & BM;
bx1 = (bx0 + 1) & BM;
rx0 = t - int(t);
rx1 = rx0 - 1.0;
t = vec[1] + N;
by0 = int(t) & BM;
by1 = (by0 + 1) & BM;
ry0 = t - int(t);
ry1 = ry0 - 1.0;
t = vec[2] + N;
bz0 = int(t) & BM;
bz1 = (bz0 + 1) & BM;
rz0 = t - int(t);
rz1 = rz0 - 1.0;
i = p[bx0];
j = p[bx1];
b00 = p[i + by0];
b10 = p[j + by0];
b01 = p[i + by1];
b11 = p[j + by1];
t = sCurve(rx0);
sy = sCurve(ry0);
sz = sCurve(rz0);
q = g3[b00 + bz0];
u = at3(q, rx0, ry0, rz0);
q = g3[b10 + bz0];
v = at3(q, rx1, ry0, rz0);
a = lerp(u, v, t);
q = g3[b01 + bz0];
u = at3(q, rx0, ry1, rz0);
q = g3[b11 + bz0];
v = at3(q, rx1, ry1, rz0);
b = lerp(u, v, t);
c = lerp(a, b, sy);
q = g3[b00 + bz1];
u = at3(q, rx0, ry0, rz1);
q = g3[b10 + bz1];
v = at3(q, rx1, ry0, rz1);
a = lerp(u, v, t);
q = g3[b01 + bz1];
u = at3(q, rx0, ry1, rz1);
q = g3[b11 + bz1];
v = at3(q, rx1, ry1, rz1);
b = lerp(u, v, t);
d = lerp(a, b, sy);
return lerp(c, d, sz);
}
// In what follows "nalpha" is the weight when the sum is formed.
// Typically it is 2, as this approaches 1 the function is noisier.
// "nbeta" is the harmonic scaling/spacing, typically 2. n is the
// number of harmonics added up in the final result. Higher number
// results in more detailed noise.
float noise1D(float x, float nalpha, float nbeta, int n) {
float val, sum = 0;
float v[] = {x};
float nscale = 1;
for (int i = 0; i < n; i++) {
val = noise1(v);
sum += val / nscale;
nscale *= nalpha;
v[0] *= nbeta;
}
return sum;
}
float noise2D(float x, float y, float nalpha, float nbeta, int n) {
float val,sum = 0;
float v[] = {x, y};
float nscale = 1;
for (int i = 0; i < n; i++) {
val = noise2(v);
sum += val / nscale;
nscale *= nalpha;
v[0] *= nbeta;
v[1] *= nbeta;
}
return sum;
}
float noise3D(float x, float y, float z, float nalpha, float nbeta, int n) {
float val, sum = 0;
float v[] = {x, y, z};
float nscale = 1;
for (int i = 0 ; i < n; i++) {
val = noise3(v);
sum += val / nscale;
nscale *= nalpha;
v[0] *= nbeta;
v[1] *= nbeta;
v[2] *= nbeta;
}
return sum;
}
}

View File

@@ -0,0 +1,122 @@
// Planets, by Andres Colubri
//
// Sun and mercury textures from http://planetpixelemporium.com
// Star field picture from http://www.galacticimages.com/
PImage starfield;
PShape sun;
PImage suntex;
PShape planet1;
PImage surftex1;
PImage cloudtex;
PShape planet2;
PImage surftex2;
void setup() {
size(800, 480, P3D);
starfield = loadImage("starfield.jpg");
suntex = loadImage("sun.jpg");
surftex1 = loadImage("planet.jpg");
// We need trilinear sampling for this texture so it looks good
// even when rendered very small.
//PTexture.Parameters params1 = PTexture.newParameters(ARGB, TRILINEAR);
surftex2 = loadImage("mercury.jpg");
/*
// The clouds texture will "move" having the values of its u
// texture coordinates displaced by adding a constant increment
// in each frame. This requires REPEAT wrapping mode so texture
// coordinates can be larger than 1.
//PTexture.Parameters params2 = PTexture.newParameters();
//params2.wrapU = REPEAT;
cloudtex = createImage(512, 256);
// Using 3D Perlin noise to generate a clouds texture that is seamless on
// its edges so it can be applied on a sphere.
cloudtex.loadPixels();
Perlin perlin = new Perlin();
for (int j = 0; j < cloudtex.height; j++) {
for (int i = 0; i < cloudtex.width; i++) {
// The angle values corresponding to each u,v pair:
float u = float(i) / cloudtex.width;
float v = float(j) / cloudtex.height;
float phi = map(u, 0, 1, TWO_PI, 0);
float theta = map(v, 0, 1, -HALF_PI, HALF_PI);
// The x, y, z point corresponding to these angles:
float x = cos(phi) * cos(theta);
float y = sin(theta);
float z = sin(phi) * cos(theta);
float n = perlin.noise3D(x, y, z, 1.2, 2, 8);
cloudtex.pixels[j * cloudtex.width + i] = color(255, 255, 255, 255 * n * n);
}
}
cloudtex.updatePixels();
*/
noStroke();
fill(255);
sphereDetail(40);
sun = createShape(SPHERE, 150);
sun.texture(suntex);
planet1 = createShape(SPHERE, 150);
planet1.texture(surftex1);
planet2 = createShape(SPHERE, 50);
planet2.texture(surftex2);
}
void draw() {
// Even we draw a full screen image after this, it is recommended to use
// background to clear the screen anyways, otherwise A3D will think
// you want to keep each drawn frame in the framebuffer, which results in
// slower rendering.
background(0);
// Disabling writing to the depth mask so the
// background image doesn't occludes any 3D object.
hint(DISABLE_DEPTH_MASK);
image(starfield, 0, 0, width, height);
hint(ENABLE_DEPTH_MASK);
/*
// Displacing the u texture coordinate of layer 1 in planet
// so it creates the effect of moving clouds.
PShape3D p = (PShape3D)planet1;
p.loadTexcoords(1);
for (int i = 0; i < p.getVertexCount(); i++) {
float u = p.texcoords[2 * i + 0];
u += 0.002;
p.texcoords[2 * i + 0] = u;
}
p.updateTexcoords();
*/
pushMatrix();
translate(width/2, height/2, -300);
pushMatrix();
rotateY(PI * frameCount / 500);
shape(sun);
popMatrix();
pointLight(255, 255, 255, 0, 0, 0);
rotateY(PI * frameCount / 300);
translate(0, 0, 300);
shape(planet2);
popMatrix();
noLights();
pointLight(255, 255, 255, 0, 0, -150);
translate(0.75 * width, 0.6 * height, 50);
shape(planet1);
}

View File

@@ -0,0 +1,190 @@
// Ariel and V3ga's arcball class with a couple tiny mods by Robert Hodgin
class Arcball {
float center_x, center_y, radius;
Vec3 v_down, v_drag;
Quat q_now, q_down, q_drag;
Vec3[] axisSet;
int axis;
float mxv, myv;
float x, y;
Arcball(float center_x, float center_y, float radius){
this.center_x = center_x;
this.center_y = center_y;
this.radius = radius;
v_down = new Vec3();
v_drag = new Vec3();
q_now = new Quat();
q_down = new Quat();
q_drag = new Quat();
axisSet = new Vec3[] {new Vec3(1.0f, 0.0f, 0.0f), new Vec3(0.0f, 1.0f, 0.0f), new Vec3(0.0f, 0.0f, 1.0f)};
axis = -1; // no constraints...
}
void mousePressed(){
v_down = mouse_to_sphere(mouseX, mouseY);
q_down.set(q_now);
q_drag.reset();
}
void mouseDragged(){
v_drag = mouse_to_sphere(mouseX, mouseY);
q_drag.set(Vec3.dot(v_down, v_drag), Vec3.cross(v_down, v_drag));
}
void run(){
q_now = Quat.mul(q_drag, q_down);
applyQuat2Matrix(q_now);
x += mxv;
y += myv;
mxv -= mxv * .01;
myv -= myv * .01;
}
Vec3 mouse_to_sphere(float x, float y){
Vec3 v = new Vec3();
v.x = (x - center_x) / radius;
v.y = (y - center_y) / radius;
float mag = v.x * v.x + v.y * v.y;
if (mag > 1.0f){
v.normalize();
} else {
v.z = sqrt(1.0f - mag);
}
return (axis == -1) ? v : constrain_vector(v, axisSet[axis]);
}
Vec3 constrain_vector(Vec3 vector, Vec3 axis){
Vec3 res = new Vec3();
res.sub(vector, Vec3.mul(axis, Vec3.dot(axis, vector)));
res.normalize();
return res;
}
void applyQuat2Matrix(Quat q){
// instead of transforming q into a matrix and applying it...
float[] aa = q.getValue();
rotate(aa[0], aa[1], aa[2], aa[3]);
}
}
static class Vec3{
float x, y, z;
Vec3(){
}
Vec3(float x, float y, float z){
this.x = x;
this.y = y;
this.z = z;
}
void normalize(){
float length = length();
x /= length;
y /= length;
z /= length;
}
float length(){
return (float) Math.sqrt(x * x + y * y + z * z);
}
static Vec3 cross(Vec3 v1, Vec3 v2){
Vec3 res = new Vec3();
res.x = v1.y * v2.z - v1.z * v2.y;
res.y = v1.z * v2.x - v1.x * v2.z;
res.z = v1.x * v2.y - v1.y * v2.x;
return res;
}
static float dot(Vec3 v1, Vec3 v2){
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
static Vec3 mul(Vec3 v, float d){
Vec3 res = new Vec3();
res.x = v.x * d;
res.y = v.y * d;
res.z = v.z * d;
return res;
}
void sub(Vec3 v1, Vec3 v2){
x = v1.x - v2.x;
y = v1.y - v2.y;
z = v1.z - v2.z;
}
}
static class Quat{
float w, x, y, z;
Quat(){
reset();
}
Quat(float w, float x, float y, float z){
this.w = w;
this.x = x;
this.y = y;
this.z = z;
}
void reset(){
w = 1.0f;
x = 0.0f;
y = 0.0f;
z = 0.0f;
}
void set(float w, Vec3 v){
this.w = w;
x = v.x;
y = v.y;
z = v.z;
}
void set(Quat q){
w = q.w;
x = q.x;
y = q.y;
z = q.z;
}
static Quat mul(Quat q1, Quat q2){
Quat res = new Quat();
res.w = q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z;
res.x = q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y;
res.y = q1.w * q2.y + q1.y * q2.w + q1.z * q2.x - q1.x * q2.z;
res.z = q1.w * q2.z + q1.z * q2.w + q1.x * q2.y - q1.y * q2.x;
return res;
}
float[] getValue(){
// transforming this quat into an angle and an axis vector...
float[] res = new float[4];
float sa = (float) Math.sqrt(1.0f - w * w);
if (sa < EPSILON){
sa = 1.0f;
}
res[0] = (float) Math.acos(w) * 2.0f;
res[1] = x / sa;
res[2] = y / sa;
res[3] = z / sa;
return res;
}
}

View File

@@ -0,0 +1,307 @@
final int MAX_BEZIER_ORDER = 10; // Maximum curve order.
final float[][] BSplineMatrix = {
{-1.0/6.0, 1.0/2.0, -1.0/2.0, 1.0/6.0},
{ 1.0/2.0, -1.0, 1.0/2.0, 0.0},
{-1.0/2.0, 0.0, 1.0/2.0, 0.0},
{ 1.0/6.0, 2.0/3.0, 1.0/6.0, 0.0}
};
// The element(i, n) of this array contains the binomial coefficient
// C(i, n) = n!/(i!(n-i)!)
final int[][] BinomialCoefTable = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{0, 1, 3, 6, 10, 15, 21, 28, 36, 45},
{0, 0, 1, 4, 10, 20, 35, 56, 84, 120},
{0, 0, 0, 1, 5, 15, 35, 70, 126, 210},
{0, 0, 0, 0, 1, 6, 21, 56, 126, 252},
{0, 0, 0, 0, 0, 1, 7, 28, 84, 210},
{0, 0, 0, 0, 0, 0, 1, 8, 36, 120},
{0, 0, 0, 0, 0, 0, 0, 1, 9, 45},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 10},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
};
// The element of this(i, j) of this table contains(i/10)^(3-j).
final float[][] TVectorTable = {
// t^3, t^2, t^1, t^0
{ 0, 0, 0, 1}, // t = 0.0
{0.001, 0.01, 0.1, 1}, // t = 0.1
{0.008, 0.04, 0.2, 1}, // t = 0.2
{0.027, 0.09, 0.3, 1}, // t = 0.3
{0.064, 0.16, 0.4, 1}, // t = 0.4
{0.125, 0.25, 0.5, 1}, // t = 0.5
{0.216, 0.36, 0.6, 1}, // t = 0.6
{0.343, 0.49, 0.7, 1}, // t = 0.7
{0.512, 0.64, 0.8, 1}, // u = 0.8
{0.729, 0.81, 0.9, 1}, // t = 0.9
{ 1, 1, 1, 1} // t = 1.0
};
// The element of this(i, j) of this table contains(3-j)*(i/10)^(2-j) if
// j < 3, 0 otherwise.
final float[][] DTVectorTable = {
// 3t^2, 2t^1, t^0
{ 0, 0, 1, 0}, // t = 0.0
{0.03, 0.2, 1, 0}, // t = 0.1
{0.12, 0.4, 1, 0}, // t = 0.2
{0.27, 0.6, 1, 0}, // t = 0.3
{0.48, 0.8, 1, 0}, // t = 0.4
{0.75, 1.0, 1, 0}, // t = 0.5
{1.08, 1.2, 1, 0}, // t = 0.6
{1.47, 1.4, 1, 0}, // t = 0.7
{1.92, 1.6, 1, 0}, // t = 0.8
{2.43, 1.8, 1, 0}, // t = 0.9
{ 3, 2, 1, 0} // t = 1.0
};
abstract class Curve3D {
abstract void feval(float t, PVector p);
abstract void deval(float t, PVector d);
abstract float fevalX(float t);
abstract float fevalY(float t);
abstract float fevalZ(float t);
abstract float devalX(float t);
abstract float devalY(float t);
abstract float devalZ(float t);
}
abstract class Spline extends Curve3D {
// The factorial of n.
int factorial(int n) {
return n <= 0 ? 1 : n * factorial(n - 1);
}
// Gives n!/(i!(n-i)!).
int binomialCoef(int i, int n) {
if ((i <= MAX_BEZIER_ORDER) && (n <= MAX_BEZIER_ORDER)) return BinomialCoefTable[i][n - 1];
else return int(factorial(n) / (factorial(i) * factorial(n - i)));
}
// Evaluates the Berstein polinomial(i, n) at u.
float bersteinPol(int i, int n, float u) {
return binomialCoef(i, n) * pow(u, i) * pow(1 - u, n - i);
}
// The derivative of the Berstein polinomial.
float dbersteinPol(int i, int n, float u) {
float s1, s2;
if (i == 0) s1 = 0;
else s1 = i * pow(u, i-1) * pow(1 - u, n - i);
if (n == i) s2 = 0;
else s2 = -(n - i) * pow(u, i) * pow(1 - u, n - i - 1);
return binomialCoef(i, n) *(s1 + s2);
}
}
class BSpline extends Spline {
// Control points.
float[][] bsplineCPoints;
// Parameters.
boolean lookup;
// Auxiliary arrays used in the calculations.
float[][] M3;
float[] TVector, DTVector;
// Point and tangent vectors.
float[] pt, tg;
BSpline() {
initParameters(true);
}
BSpline(boolean t) {
initParameters(t);
}
// Sets lookup table use.
void initParameters(boolean t) {
bsplineCPoints = new float[4][3];
TVector = new float[4];
DTVector = new float[4];
M3 = new float[4][3];
pt = new float[3];
tg = new float[3];
lookup = t;
}
// Sets n-th control point.
void setCPoint(int n, PVector P) {
bsplineCPoints[n][0] = P.x;
bsplineCPoints[n][1] = P.y;
bsplineCPoints[n][2] = P.z;
updateMatrix3();
}
// Gets n-th control point.
void getCPoint(int n, PVector P) {
P.set(bsplineCPoints[n]);
}
// Replaces the current B-spline control points(0, 1, 2) with(1, 2, 3). This
// is used when a new spline is to be joined to the recently drawn.
void shiftBSplineCPoints() {
for (int i = 0; i < 3; i++) {
bsplineCPoints[0][i] = bsplineCPoints[1][i];
bsplineCPoints[1][i] = bsplineCPoints[2][i];
bsplineCPoints[2][i] = bsplineCPoints[3][i];
}
updateMatrix3();
}
void copyCPoints(int n_source, int n_dest) {
for (int i = 0; i < 3; i++) {
bsplineCPoints[n_dest][i] = bsplineCPoints[n_source][i];
}
}
// Updates the temporal matrix used in order 3 calculations.
void updateMatrix3() {
float s;
int i, j, k;
for(i = 0; i < 4; i++) {
for(j = 0; j < 3; j++) {
s = 0;
for(k = 0; k < 4; k++) s += BSplineMatrix[i][k] * bsplineCPoints[k][j];
M3[i][j] = s;
}
}
}
void feval(float t, PVector p) {
evalPoint(t);
p.set(pt);
}
void deval(float t, PVector d) {
evalTangent(t);
d.set(tg);
}
float fevalX(float t) {
evalPoint(t);
return pt[0];
}
float fevalY(float t) {
evalPoint(t);
return pt[1];
}
float fevalZ(float t) {
evalPoint(t);
return pt[2];
}
float devalX(float t) {
evalTangent(t);
return tg[0];
}
float devalY(float t) {
evalTangent(t);
return tg[1];
}
float devalZ(float t) {
evalTangent(t);
return tg[2];
}
// Point evaluation.
void evalPoint(float t) {
if (lookup) {
bsplinePointI(int(10 * t));
} else {
bsplinePoint(t);
}
}
// Tangent evaluation.
void evalTangent(float t) {
if (lookup) {
bsplineTangentI(int(10 * t));
} else {
bsplineTangent(t);
}
}
// Calculates the point on the cubic spline corresponding to the parameter value t in [0, 1].
void bsplinePoint(float t) {
// Q(u) = UVector * BSplineMatrix * BSplineCPoints
float s;
int i, j, k;
for(i = 0; i < 4; i++) {
TVector[i] = pow(t, 3 - i);
}
for(j = 0; j < 3; j++) {
s = 0;
for(k = 0; k < 4; k++) {
s += TVector[k] * M3[k][j];
}
pt[j] = s;
}
}
// Calculates the tangent vector of the spline at t.
void bsplineTangent(float t) {
// Q(u) = DTVector * BSplineMatrix * BSplineCPoints
float s;
int i, j, k;
for(i = 0; i < 4; i++) {
if (i < 3) {
DTVector[i] = (3 - i) * pow(t, 2 - i);
} else {
DTVector[i] = 0;
}
}
for(j = 0; j < 3; j++) {
s = 0;
for(k = 0; k < 4; k++) {
s += DTVector[k] * M3[k][j];
}
tg[j] = s;
}
}
// Gives the point on the cubic spline corresponding to t/10(using the lookup table).
void bsplinePointI(int t) {
// Q(u) = TVectorTable[u] * BSplineMatrix * BSplineCPoints
float s;
int j, k;
for(j = 0; j < 3; j++) {
s = 0;
for(k = 0; k < 4; k++) {
s += TVectorTable[t][k] * M3[k][j];
}
pt[j] = s;
}
}
// Calulates the tangent vector of the spline at t/10.
void bsplineTangentI(int t) {
// Q(u) = DTVectorTable[u] * BSplineMatrix * BSplineCPoints
float s;
int j, k;
for(j = 0; j < 3; j++) {
s = 0;
for(k = 0; k < 4; k++) {
s += DTVectorTable[t][k] * M3[k][j];
}
tg[j] = s;
}
}
}

View File

@@ -0,0 +1,495 @@
BSpline splineSide1;
BSpline splineCenter;
BSpline splineSide2;
PVector flipTestV;
int uspacing;
int HELIX = 0;
int STRAND = 1;
int COIL = 2;
int LHANDED = -1;
int RHANDED = 1;
void createRibbonModel(ArrayList residues, PShape model, ArrayList trj) {
// For line ribbons
ArrayList vertices0 = new ArrayList();
ArrayList vertices1 = new ArrayList();
ArrayList vertices2 = new ArrayList();
// For flat ribbons
ArrayList vertices = new ArrayList();
ArrayList normals = new ArrayList();
if (ribbonDetail == 1) uspacing = 10;
else if (ribbonDetail == 2) uspacing = 5;
else if (ribbonDetail == 3) uspacing = 2;
else uspacing = 1;
flipTestV = new PVector();
splineSide1 = new BSpline(false);
splineCenter = new BSpline(false);
splineSide2 = new BSpline(false);
int[] ss = new int[residues.size()];
int[] handness = new int[residues.size()];
calculateSecStr(residues, ss, handness);
for (int i = 0; i < residues.size(); i++) {
constructControlPoints(residues, i, ss[i], handness[i]);
if (renderMode == 0) {
generateSpline(0, vertices0);
generateSpline(1, vertices1);
generateSpline(2, vertices2);
}
else {
generateFlatRibbon(vertices, normals);
}
}
if (renderMode == 0) {
model = createShape();
model.stroke(ribbonColor);
model.noFill();
model.beginContour();
for (int i = 0; i < vertices0.size(); i++) {
PVector posVec = (PVector)vertices0.get(i);
model.vertex(posVec.x, posVec.y, posVec.z);
}
model.endContour();
model.beginContour();
for (int i = 0; i < vertices1.size(); i++) {
PVector posVec = (PVector)vertices1.get(i);
model.vertex(posVec.x, posVec.y, posVec.z);
}
model.endContour();
model.beginContour();
for (int i = 0; i < vertices2.size(); i++) {
PVector posVec = (PVector)vertices2.get(i);
model.vertex(posVec.x, posVec.y, posVec.z);
}
model.endContour();
model.end(OPEN);
} else {
// The ribbon construction is fairly inneficient here, since
// it could use triangle strips instead to avoid duplicating
// shared vertices...
model = createShape(TRIANGLES);
model.noStroke();
model.fill(ribbonColor);
for (int i = 0; i < vertices.size(); i++) {
PVector posVec = (PVector)vertices.get(i);
PVector normVec = (PVector)normals.get(i);
model.normal(-normVec.x, -normVec.y, -normVec.z);
model.vertex(posVec.x, posVec.y, posVec.z);
}
model.end();
}
trj.add(model);
if (renderMode == 0) {
int totCount = vertices0.size() + vertices1.size() + vertices2.size();
println("Adding new model with " + totCount + " vertices.");
} else {
println("Adding new model with " + vertices.size() + " vertices.");
}
}
float calculateGyrRadius(ArrayList atoms) {
PVector ati, atj;
float dx, dy, dz;
float r = 0;
for (int i = 0; i < atoms.size(); i++) {
ati = (PVector)atoms.get(i);
for (int j = i + 1; j < atoms.size(); j++) {
atj = (PVector)atoms.get(j);
dx = ati.x - atj.x;
dy = ati.y - atj.y;
dz = ati.z - atj.z;
r += dx * dx + dy * dy + dz * dz;
}
}
return sqrt(r) / (atoms.size() + 1);
}
// Does a cheap and dirty secondary structure assignment to the protein
// residues given in the array.
void calculateSecStr(ArrayList residues, int[] ss, int[] handness) {
PVector c0, n1, ca1, c1, n2;
HashMap res0, res1, res2;
int n = residues.size();
float[] phi = new float[n];
float[] psi = new float[n];
for (int i = 0; i < n; i++) {
if (i == 0 || i == n - 1) {
phi[i] = 90;
psi[i] = 90;
} else {
res0 = (HashMap)residues.get(i - 1);
res1 = (HashMap)residues.get(i);
res2 = (HashMap)residues.get(i + 1);
c0 = (PVector)res0.get("C");
n1 = (PVector)res1.get("N");
ca1 = (PVector)res1.get("CA");
c1 = (PVector)res1.get("C");
n2 = (PVector)res2.get("N");
phi[i] = calculateTorsionalAngle(c0, n1, ca1, c1);
psi[i] = calculateTorsionalAngle(n1, ca1, c1, n2);
}
}
int firstHelix = 0;
int nconsRHelix = 0;
int nconsLHelix = 0;
int firstStrand = 0;
int nconsStrand = 0;
for (int i = 0; i < n; i++) {
// Right-handed helix
if ((dist(phi[i], psi[i], -60, -45) < 30) && (i < n - 1)) {
if (nconsRHelix == 0) firstHelix = i;
nconsRHelix++;
}
else {
if (3 <= nconsRHelix) {
for (int k = firstHelix; k < i; k++) {
ss[k] = HELIX;
handness[k] = RHANDED;
}
}
nconsRHelix = 0;
}
// Left-handed helix
if ((dist(phi[i], psi[i], +60, +45) < 30) && (i < n - 1)) {
if (nconsLHelix == 0) firstHelix = i;
nconsLHelix++;
} else {
if (3 <= nconsLHelix) {
for (int k = firstHelix; k < i; k++) {
ss[k] = HELIX;
handness[k] = LHANDED;
}
}
nconsLHelix = 0;
}
// Strand
if ((dist(phi[i], psi[i], -110, +130) < 30) && (i < n - 1)) {
if (nconsStrand == 0) firstStrand = i;
nconsStrand++;
} else {
if (2 <= nconsStrand) {
for (int k = firstStrand; k < i; k++) {
ss[k] = STRAND;
handness[k] = RHANDED;
}
}
nconsStrand = 0;
}
ss[i] = COIL;
handness[i] = RHANDED;
}
}
// Calculates the torsional angle defined by four atoms with positions at0, at1, at2 and at3.
float calculateTorsionalAngle(PVector at0, PVector at1, PVector at2, PVector at3) {
PVector r01 = PVector.sub(at0, at1);
PVector r32 = PVector.sub(at3, at2);
PVector r12 = PVector.sub(at1, at2);
PVector p = r12.cross(r01);
PVector q = r12.cross(r32);
PVector r = r12.cross(q);
float u = q.dot(q);
float v = r.dot(r);
float a;
if (u <= 0.0 || v <= 0.0) {
a = 360.0;
} else {
float u1 = p.dot(q); // u1 = p * q
float v1 = p.dot(r); // v1 = p * r
u = u1 / sqrt(u);
v = v1 / sqrt(v);
if (abs(u) > 0.01 || abs(v) > 0.01) a = degrees(atan2(v, u));
else a = 360.0;
}
return a;
}
void generateSpline(int n, ArrayList vertices) {
int ui;
float u;
PVector v0, v1;
v1 = new PVector();
if (n == 0) splineSide1.feval(0, v1);
else if (n == 1) splineCenter.feval(0, v1);
else splineSide2.feval(0, v1);
vertices.add(new PVector(v1.x, v1.y, v1.z));
for (ui = 1; ui <= 10; ui ++) {
if (ui % uspacing == 0) {
u = 0.1 * ui;
if (n == 0) splineSide1.feval(u, v1);
else if (n == 1) splineCenter.feval(u, v1);
else splineSide2.feval(u, v1);
vertices.add(new PVector(v1.x, v1.y, v1.z));
}
}
}
void generateFlatRibbon(ArrayList vertices, ArrayList normals) {
PVector CentPoint0, CentPoint1;
PVector Sid1Point0, Sid1Point1;
PVector Sid2Point0, Sid2Point1;
PVector Transversal, Tangent;
PVector Normal0, Normal1;
int ui;
float u;
CentPoint0 = new PVector();
CentPoint1 = new PVector();
Sid1Point0 = new PVector();
Sid1Point1 = new PVector();
Sid2Point0 = new PVector();
Sid2Point1 = new PVector();
Transversal = new PVector();
Tangent = new PVector();
Normal0 = new PVector();
Normal1 = new PVector();
// The initial geometry is generated.
splineSide1.feval(0, Sid1Point1);
splineCenter.feval(0, CentPoint1);
splineSide2.feval(0, Sid2Point1);
// The tangents at the three previous points are the same.
splineSide2.deval(0, Tangent);
// Vector transversal to the ribbon.
Transversal = PVector.sub(Sid1Point1, Sid2Point1);
// The normal is calculated.
Normal1 = Transversal.cross(Tangent);
Normal1.normalize();
for (ui = 1; ui <= 10; ui ++) {
if (ui % uspacing == 0) {
u = 0.1 * ui;
// The geometry of the previous iteration is saved.
Sid1Point0.set(Sid1Point1);
CentPoint0.set(CentPoint1);
Sid2Point0.set(Sid2Point1);
Normal0.set(Normal1);
// The new geometry is generated.
splineSide1.feval(u, Sid1Point1);
splineCenter.feval(u, CentPoint1);
splineSide2.feval(u, Sid2Point1);
// The tangents at the three previous points are the same.
splineSide2.deval(u, Tangent);
// Vector transversal to the ribbon.
Transversal = PVector.sub(Sid1Point1, Sid2Point1);
// The normal is calculated.
Normal1 = Transversal.cross(Tangent);
Normal1.normalize();
// The (Sid1Point0, Sid1Point1, CentPoint1) triangle is added.
vertices.add(new PVector(Sid1Point0.x, Sid1Point0.y, Sid1Point0.z));
normals.add(new PVector(Normal0.x, Normal0.y, Normal0.z));
vertices.add(new PVector(Sid1Point1.x, Sid1Point1.y, Sid1Point1.z));
normals.add(new PVector(Normal1.x, Normal1.y, Normal1.z));
vertices.add(new PVector(CentPoint1.x, CentPoint1.y, CentPoint1.z));
normals.add(new PVector(Normal1.x, Normal1.y, Normal1.z));
// The (Sid1Point0, CentPoint1, CentPoint0) triangle is added.
vertices.add(new PVector(Sid1Point0.x, Sid1Point0.y, Sid1Point0.z));
normals.add(new PVector(Normal0.x, Normal0.y, Normal0.z));
vertices.add(new PVector(CentPoint1.x, CentPoint1.y, CentPoint1.z));
normals.add(new PVector(Normal1.x, Normal1.y, Normal1.z));
vertices.add(new PVector(CentPoint0.x, CentPoint0.y, CentPoint0.z));
normals.add(new PVector(Normal0.x, Normal0.y, Normal0.z));
// (Sid2Point0, Sid2Point1, CentPoint1) triangle is added.
vertices.add(new PVector(Sid2Point0.x, Sid2Point0.y, Sid2Point0.z));
normals.add(new PVector(Normal0.x, Normal0.y, Normal0.z));
vertices.add(new PVector(Sid2Point1.x, Sid2Point1.y, Sid2Point1.z));
normals.add(new PVector(Normal1.x, Normal1.y, Normal1.z));
vertices.add(new PVector(CentPoint1.x, CentPoint1.y, CentPoint1.z));
normals.add(new PVector(Normal1.x, Normal1.y, Normal1.z));
// (Sid2Point0, CentPoint1, CentPoint0) triangle is added.
vertices.add(new PVector(Sid2Point0.x, Sid2Point0.y, Sid2Point0.z));
normals.add(new PVector(Normal0.x, Normal0.y, Normal0.z));
vertices.add(new PVector(CentPoint1.x, CentPoint1.y, CentPoint1.z));
normals.add(new PVector(Normal1.x, Normal1.y, Normal1.z));
vertices.add(new PVector(CentPoint0.x, CentPoint0.y, CentPoint0.z));
normals.add(new PVector(Normal0.x, Normal0.y, Normal0.z));
}
}
}
/******************************************************************************
* The code in the following three functions is based in the method introduced
* in this paper:
* "Algorithm for ribbon models of proteins."
* Authors: Mike Carson and Charles E. Bugg
* Published in: J.Mol.Graphics 4, pp. 121-122 (1986)
******************************************************************************/
// Shifts the control points one place to the left.
void shiftControlPoints() {
splineSide1.shiftBSplineCPoints();
splineCenter.shiftBSplineCPoints();
splineSide2.shiftBSplineCPoints();
}
// Adds a new control point to the arrays CPCenter, CPRight and CPLeft
void addControlPoints(PVector ca0, PVector ox0, PVector ca1, int ss, int handness) {
PVector A, B, C, D, p0, cpt0, cpt1, cpt2;
A = PVector.sub(ca1, ca0);
B = PVector.sub(ox0, ca0);
// Vector normal to the peptide plane (pointing outside in the case of the
// alpha helix).
C = A.cross(B);
// Vector contained in the peptide plane (perpendicular to its direction).
D = C.cross(A);
// Normalizing vectors.
C.normalize();
D.normalize();
// Flipping test (to avoid self crossing in the strands).
if ((ss != HELIX) && (90.0 < degrees(PVector.angleBetween(flipTestV, D)))) {
// Flip detected. The plane vector is inverted.
D.mult(-1.0);
}
// The central control point is constructed.
cpt0 = linearComb(0.5, ca0, 0.5, ca1);
splineCenter.setCPoint(3, cpt0);
if (ss == HELIX) {
// When residue i is contained in a helix, the control point is moved away
// from the helix axis, along the C direction.
p0 = new PVector();
splineCenter.getCPoint(3, p0);
cpt0 = linearComb(1.0, p0, handness * helixDiam, C);
splineCenter.setCPoint(3, cpt0);
}
// The control points for the side ribbons are constructed.
cpt1 = linearComb(1.0, cpt0, +ribbonWidth[ss], D);
splineSide1.setCPoint(3, cpt1);
cpt2 = linearComb(1.0, cpt0, -ribbonWidth[ss], D);
splineSide2.setCPoint(3, cpt2);
// Saving the plane vector (for the flipping test in the next call).
flipTestV.set(D);
}
void constructControlPoints(ArrayList residues, int res, int ss, int handness) {
PVector ca0, ox0, ca1;
PVector p0, p1, p2, p3;
p1 = new PVector();
p2 = new PVector();
p3 = new PVector();
HashMap res0, res1;
res0 = res1 = null;
if (res == 0) {
// The control points 2 and 3 are created.
flipTestV.set(0, 0, 0);
res0 = (HashMap)residues.get(res);
res1 = (HashMap)residues.get(res + 1);
ca0 = (PVector)res0.get("CA");
ox0 = (PVector)res0.get("O");
ca1 = (PVector)res1.get("CA");
addControlPoints(ca0, ox0, ca1, ss, handness);
splineSide1.copyCPoints(3, 2);
splineCenter.copyCPoints(3, 2);
splineSide2.copyCPoints(3, 2);
res0 = (HashMap)residues.get(res + 1);
res1 = (HashMap)residues.get(res + 2);
ca0 = (PVector)res0.get("CA");
ox0 = (PVector)res0.get("O");
ca1 = (PVector)res1.get("CA");
addControlPoints(ca0, ox0, ca1, ss, handness);
// We still need the two first control points.
// Moving backwards along the cp_center[2] - cp_center[3] direction.
splineCenter.getCPoint(2, p2);
splineCenter.getCPoint(3, p3);
p1 = linearComb(2.0, p2, -1, p3);
splineCenter.setCPoint(1, p1);
splineSide1.setCPoint(1, linearComb(1.0, p1, +ribbonWidth[ss], flipTestV));
splineSide2.setCPoint(1, linearComb(1.0, p1, -ribbonWidth[ss], flipTestV));
p0 = linearComb(2.0, p1, -1, p2);
splineCenter.setCPoint(0, p0);
splineSide1.setCPoint(0, linearComb(1.0, p0, +ribbonWidth[ss], flipTestV));
splineSide2.setCPoint(0, linearComb(1.0, p0, -ribbonWidth[ss], flipTestV));
} else {
shiftControlPoints();
if ((residues.size() - 1 == res) || (residues.size() - 2 == res)) {
// Moving forward along the cp_center[1] - cp_center[2] direction.
splineCenter.getCPoint(1, p1);
splineCenter.getCPoint(2, p2);
p3 = linearComb(2.0, p2, -1, p1);
splineCenter.setCPoint(3, p3);
splineSide1.setCPoint(3, linearComb(1.0, p3, +ribbonWidth[ss], flipTestV));
splineSide2.setCPoint(3, linearComb(1.0, p3, -ribbonWidth[ss], flipTestV));
} else {
res0 = (HashMap)residues.get(res + 1);
res1 = (HashMap)residues.get(res + 2);
ca0 = (PVector)res0.get("CA");
ox0 = (PVector)res0.get("O");
ca1 = (PVector)res1.get("CA");
addControlPoints(ca0, ox0, ca1, ss, handness);
}
}
splineSide1.updateMatrix3();
splineCenter.updateMatrix3();
splineSide2.updateMatrix3();
}
PVector linearComb(float scalar0, PVector vector0, float scalar1, PVector vector1) {
return PVector.add(PVector.mult(vector0, scalar0), PVector.mult(vector1, scalar1));
}

View File

@@ -0,0 +1,115 @@
void readPDB(String filename) {
String strLines[];
float xmin, xmax, ymin, ymax, zmin, zmax;
String xstr, ystr, zstr;
float x, y, z;
int res, res0;
int nmdl;
String atstr, resstr;
PShape model;
ArrayList atoms;
ArrayList residues;
HashMap residue;
PVector v;
String s;
strLines = loadStrings(filename);
models = new ArrayList();
xmin = ymin = zmin = 10000;
xmax = ymax = zmax = -10000;
atoms = null;
residues = null;
residue = null;
model = null;
res0 = -1;
nmdl = -1;
for (int i = 0; i < strLines.length; i++) {
s = strLines[i];
if (s.startsWith("MODEL") || (s.startsWith("ATOM") && res0 == -1)) {
nmdl++;
res0 = -1;
atoms = new ArrayList();
residues = new ArrayList();
}
if (s.startsWith("ATOM")) {
atstr = s.substring(12, 15);
atstr = atstr.trim();
resstr = s.substring(22, 26);
resstr = resstr.trim();
res = parseInt(resstr);
xstr = s.substring(30, 37);
xstr = xstr.trim();
ystr = s.substring(38, 45);
ystr = ystr.trim();
zstr = s.substring(46, 53);
zstr = zstr.trim();
x = scaleFactor * parseFloat(xstr);
y = scaleFactor * parseFloat(ystr);
z = scaleFactor * parseFloat(zstr);
v = new PVector(x, y, z);
xmin = min(xmin, x);
xmax = max(xmax, x);
ymin = min(ymin, y);
ymax = max(ymax, y);
zmin = min(zmin, z);
zmax = max(zmax, z);
atoms.add(v);
if (res0 != res) {
if (residue != null) residues.add(residue);
residue = new HashMap();
}
residue.put(atstr, v);
res0 = res;
}
if (s.startsWith("ENDMDL") || s.startsWith("TER")) {
if (residue != null) residues.add(residue);
createRibbonModel(residues, model, models);
float rgyr = calculateGyrRadius(atoms);
res0 = -1;
residue = null;
atoms = null;
residues = null;
}
}
if (residue != null) {
if (residue != null) residues.add(residue);
createRibbonModel(residues, model, models);
float rgyr = calculateGyrRadius(atoms);
atoms = null;
residues = null;
}
// Centering models at (0, 0, 0).
float dx = -0.5f * (xmin + xmax);
float dy = -0.5f * (ymin + ymax);
float dz = -0.5f * (zmin + zmax);
for (int n = 0; n < models.size(); n++) {
model = (PShape) models.get(n);
model.translate(dx, dy, dz);
}
println("Loaded PDB file with " + models.size() + " models.");
}

View File

@@ -0,0 +1,50 @@
// Ribbons, by Andres Colubri
// ArcBall class by Ariel, V3ga and Robert Hodgin (flight404)
// This sketch loads 3D atomic coordinates of a protein molecule
// from a file in PDB format (http://www.pdb.org/) and displays
// the structure using a ribbon representation.
String pdbFile = "4HHB.pdb"; // PDB file to read
//String pdbFile = "1CBS.pdb";
//String pdbFile = "2POR.pdb";
// Some parameters to control the visual appearance:
float scaleFactor = 5; // Size factor
int renderMode = 1; // 0 = lines, 1 = flat ribbons
int ribbonDetail = 4; // Ribbon detail: from 1 (lowest) to 4 (highest)
float helixDiam = 10; // Helix diameter.
int[] ribbonWidth = {10, 7, 2}; // Ribbon widths for helix, strand and coil
color ribbonColor = color(20, 30, 200, 255); // Ribbon color
// All the molecular models read from the PDB file (it could contain more than one)
ArrayList models;
Arcball arcball;
void setup() {
size(800, 600, P3D);
arcball = new Arcball(width/2, height/2, 600);
readPDB(pdbFile);
}
void draw() {
background(0);
lights();
translate(width/2, height/2, 200);
arcball.run();
for (int i = 0; i < models.size(); i++) {
shape((PShape)models.get(i));
}
}
void mousePressed(){
arcball.mousePressed();
}
void mouseDragged(){
arcball.mouseDragged();
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,104 @@
// Code to draw a trefoil knot surface, with normals and texture
// coordinates.
// Adapted from the parametric equations example by Philip Rideout:
// http://iphone-3d-programming.labs.oreilly.com/ch03.html
// This function draws a trefoil knot surface as a triangle mesh derived
// from its parametric equation.
PShape createTrefoil(float s, int ny, int nx, PImage tex) {
PVector p0, p1, p2;
PVector n0, n1, n2;
float u0, u1, v0, v1;
PShape obj = createShape(TRIANGLES);
obj.texture(tex);
for (int j = 0; j < nx; j++) {
u0 = float(j) / nx;
u1 = float(j + 1) / nx;
for (int i = 0; i < ny; i++) {
v0 = float(i) / ny;
v1 = float(i + 1) / ny;
p0 = evalPoint(u0, v0);
n0 = evalNormal(u0, v0);
p1 = evalPoint(u0, v1);
n1 = evalNormal(u0, v1);
p2 = evalPoint(u1, v1);
n2 = evalNormal(u1, v1);
// Triangle p0-p1-p2
obj.normal(n0.x, n0.y, n0.z);
obj.vertex(s * p0.x, s * p0.y, s * p0.z, u0, v0);
obj.normal(n1.x, n1.y, n1.z);
obj.vertex(s * p1.x, s * p1.y, s * p1.z, u0, v1);
obj.normal(n2.x, n2.y, n2.z);
obj.vertex(s * p2.x, s * p2.y, s * p2.z, u1, v1);
p1 = evalPoint(u1, v0);
n1 = evalNormal(u1, v0);
// Triangle p0-p2-p1
obj.normal(n0.x, n0.y, n0.z);
obj.vertex(s * p0.x, s * p0.y, s * p0.z, u0, v0);
obj.normal(n2.x, n2.y, n2.z);
obj.vertex(s * p2.x, s * p2.y, s * p2.z, u1, v1);
obj.normal(n1.x, n1.y, n1.z);
obj.vertex(s * p1.x, s * p1.y, s * p1.z, u1, v0);
}
}
obj.end();
return obj;
}
// Evaluates the surface normal corresponding to normalized
// parameters (u, v)
PVector evalNormal(float u, float v) {
// Compute the tangents and their cross product.
PVector p = evalPoint(u, v);
PVector tangU = evalPoint(u + 0.01, v);
PVector tangV = evalPoint(u, v + 0.01);
tangU.sub(p);
tangV.sub(p);
PVector normUV = tangV.cross(tangU);
normUV.normalize();
return normUV;
}
// Evaluates the surface point corresponding to normalized
// parameters (u, v)
PVector evalPoint(float u, float v) {
float a = 0.5;
float b = 0.3;
float c = 0.5;
float d = 0.1;
float s = TWO_PI * u;
float t = (TWO_PI * (1 - v)) * 2;
float r = a + b * cos(1.5 * t);
float x = r * cos(t);
float y = r * sin(t);
float z = c * sin(1.5 * t);
PVector dv = new PVector();
dv.x = -1.5 * b * sin(1.5 * t) * cos(t) -
(a + b * cos(1.5 * t)) * sin(t);
dv.y = -1.5 * b * sin(1.5 * t) * sin(t) +
(a + b * cos(1.5 * t)) * cos(t);
dv.z = 1.5 * c * cos(1.5 * t);
PVector q = dv;
q.normalize();
PVector qvn = new PVector(q.y, -q.x, 0);
qvn.normalize();
PVector ww = q.cross(qvn);
PVector pt = new PVector();
pt.x = x + d * (qvn.x * cos(s) + ww.x * sin(s));
pt.y = y + d * (qvn.y * cos(s) + ww.y * sin(s));
pt.z = z + d * ww.z * sin(s);
return pt;
}

View File

@@ -0,0 +1,42 @@
// Trefoil, by Andres Colubri
// A parametric surface is textured procedurally
// by drawing on an offscreen PGraphics surface.
PGraphics pg;
PShape trefoil;
void setup() {
size(280, 400, P3D);
textureMode(NORMAL);
noStroke();
// Creating offscreen surface for 3D rendering.
pg = createGraphics(32, 512, P3D);
pg.beginDraw();
pg.background(0, 0);
pg.noStroke();
pg.fill(255, 0, 0, 200);
pg.endDraw();
// Saving trefoil surface into a PShape3D object
trefoil = createTrefoil(250, 60, 15, pg);
}
void draw() {
background(0);
pg.beginDraw();
pg.ellipse(random(pg.width), random(pg.height), 4, 4);
pg.endDraw();
ambient(250, 250, 250);
pointLight(255, 255, 255, 0, 0, 200);
pushMatrix();
translate(width/2, height/2, -200);
rotateX(frameCount * PI / 500);
rotateY(frameCount * PI / 500);
shape(trefoil);
popMatrix();
}

View File

@@ -0,0 +1,283 @@
// Press 'w' to start wiggling, space to restore
// original positions.
PShape cube;
float cubeSize = 200;
float circleRad = 50;
int circleRes = 20;
float noiseMag = 1;
boolean wiggling = false;
void setup() {
size(400, 400, P3D);
createCube();
}
void draw() {
background(0);
translate(width/2, height/2);
rotateX(frameCount * 0.01f);
rotateY(frameCount * 0.01f);
shape(cube);
if (wiggling) {
PVector pos = null;
for (int i = 0; i < cube.getChildCount(); i++) {
PShape face = cube.getChild(i);
for (int j = 0; j < face.getVertexCount(); j++) {
pos = face.getVertex(j, pos);
pos.x += random(-noiseMag/2, +noiseMag/2);
pos.y += random(-noiseMag/2, +noiseMag/2);
pos.z += random(-noiseMag/2, +noiseMag/2);
face.setVertex(j, pos.x, pos.y, pos.z);
}
}
}
println(frameRate);
}
public void keyPressed() {
if (key == 'w') {
wiggling = !wiggling;
} else if (key == ' ') {
restoreCube();
} else if (key == '1') {
cube.strokeWeight(1);
} else if (key == '2') {
cube.strokeWeight(5);
} else if (key == '3') {
cube.strokeWeight(10);
}
}
void createCube() {
cube = createShape(GROUP);
PShape face;
// Front face
face = createShape(POLYGON);
face.stroke(255, 0, 0);
face.fill(255);
face.beginContour();
face.vertex(-cubeSize/2, -cubeSize/2, +cubeSize/2);
face.vertex(+cubeSize/2, -cubeSize/2, +cubeSize/2);
face.vertex(+cubeSize/2, +cubeSize/2, +cubeSize/2);
face.vertex(-cubeSize/2, +cubeSize/2, +cubeSize/2);
face.endContour();
face.beginContour();
for (int i = 0; i < circleRes; i++) {
float angle = TWO_PI * i / circleRes;
float x = circleRad * sin(angle);
float y = circleRad * cos(angle);
float z = +cubeSize/2;
face.vertex(x, y, z);
}
face.endContour();
face.end(CLOSE);
cube.addChild(face);
// Back face
face = createShape(POLYGON);
face.stroke(255, 0, 0);
face.fill(255);
face.beginContour();
face.vertex(+cubeSize/2, -cubeSize/2, -cubeSize/2);
face.vertex(-cubeSize/2, -cubeSize/2, -cubeSize/2);
face.vertex(-cubeSize/2, +cubeSize/2, -cubeSize/2);
face.vertex(+cubeSize/2, +cubeSize/2, -cubeSize/2);
face.endContour();
face.beginContour();
for (int i = 0; i < circleRes; i++) {
float angle = TWO_PI * i / circleRes;
float x = circleRad * sin(angle);
float y = circleRad * cos(angle);
float z = -cubeSize/2;
face.vertex(x, y, z);
}
face.endContour();
face.end(CLOSE);
cube.addChild(face);
// Right face
face = createShape(POLYGON);
face.stroke(255, 0, 0);
face.fill(255);
face.beginContour();
face.vertex(+cubeSize/2, -cubeSize/2, +cubeSize/2);
face.vertex(+cubeSize/2, -cubeSize/2, -cubeSize/2);
face.vertex(+cubeSize/2, +cubeSize/2, -cubeSize/2);
face.vertex(+cubeSize/2, +cubeSize/2, +cubeSize/2);
face.endContour();
face.beginContour();
for (int i = 0; i < circleRes; i++) {
float angle = TWO_PI * i / circleRes;
float x = +cubeSize/2;
float y = circleRad * sin(angle);
float z = circleRad * cos(angle);
face.vertex(x, y, z);
}
face.endContour();
face.end(CLOSE);
cube.addChild(face);
// Left face
face = createShape(POLYGON);
face.stroke(255, 0, 0);
face.fill(255);
face.beginContour();
face.vertex(-cubeSize/2, -cubeSize/2, -cubeSize/2);
face.vertex(-cubeSize/2, -cubeSize/2, +cubeSize/2);
face.vertex(-cubeSize/2, +cubeSize/2, +cubeSize/2);
face.vertex(-cubeSize/2, +cubeSize/2, -cubeSize/2);
face.endContour();
face.beginContour();
for (int i = 0; i < circleRes; i++) {
float angle = TWO_PI * i / circleRes;
float x = -cubeSize/2;
float y = circleRad * sin(angle);
float z = circleRad * cos(angle);
face.vertex(x, y, z);
}
face.endContour();
face.end(CLOSE);
cube.addChild(face);
// Top face
face = createShape(POLYGON);
face.stroke(255, 0, 0);
face.fill(255);
face.beginContour();
face.vertex(-cubeSize/2, +cubeSize/2, +cubeSize/2);
face.vertex(+cubeSize/2, +cubeSize/2, +cubeSize/2);
face.vertex(+cubeSize/2, +cubeSize/2, -cubeSize/2);
face.vertex(-cubeSize/2, +cubeSize/2, -cubeSize/2);
face.endContour();
face.beginContour();
for (int i = 0; i < circleRes; i++) {
float angle = TWO_PI * i / circleRes;
float x = circleRad * sin(angle);
float y = +cubeSize/2;
float z = circleRad * cos(angle);
face.vertex(x, y, z);
}
face.endContour();
face.end(CLOSE);
cube.addChild(face);
// Bottom face
face = createShape(POLYGON);
face.stroke(255, 0, 0);
face.fill(255);
face.beginContour();
face.vertex(+cubeSize/2, -cubeSize/2, +cubeSize/2);
face.vertex(-cubeSize/2, -cubeSize/2, +cubeSize/2);
face.vertex(-cubeSize/2, -cubeSize/2, -cubeSize/2);
face.vertex(+cubeSize/2, -cubeSize/2, -cubeSize/2);
face.endContour();
face.beginContour();
for (int i = 0; i < circleRes; i++) {
float angle = TWO_PI * i / circleRes;
float x = circleRad * sin(angle);
float y = -cubeSize/2;
float z = circleRad * cos(angle);
face.vertex(x, y, z);
}
face.endContour();
face.end(CLOSE);
cube.addChild(face);
}
void restoreCube() {
PShape face;
// Front face
face = cube.getChild(0);
face.setVertex(0, -cubeSize/2, -cubeSize/2, +cubeSize/2);
face.setVertex(1, +cubeSize/2, -cubeSize/2, +cubeSize/2);
face.setVertex(2, +cubeSize/2, +cubeSize/2, +cubeSize/2);
face.setVertex(3, -cubeSize/2, +cubeSize/2, +cubeSize/2);
for (int i = 0; i < circleRes; i++) {
float angle = TWO_PI * i / circleRes;
float x = circleRad * sin(angle);
float y = circleRad * cos(angle);
float z = +cubeSize/2;
face.setVertex(4 + i, x, y, z);
}
// Back face
face = cube.getChild(1);
face.setVertex(0, +cubeSize/2, -cubeSize/2, -cubeSize/2);
face.setVertex(1, -cubeSize/2, -cubeSize/2, -cubeSize/2);
face.setVertex(2, -cubeSize/2, +cubeSize/2, -cubeSize/2);
face.setVertex(3, +cubeSize/2, +cubeSize/2, -cubeSize/2);
for (int i = 0; i < circleRes; i++) {
float angle = TWO_PI * i / circleRes;
float x = circleRad * sin(angle);
float y = circleRad * cos(angle);
float z = -cubeSize/2;
face.setVertex(4 + i, x, y, z);
}
// Right face
face = cube.getChild(2);
face.setVertex(0, +cubeSize/2, -cubeSize/2, +cubeSize/2);
face.setVertex(1, +cubeSize/2, -cubeSize/2, -cubeSize/2);
face.setVertex(2, +cubeSize/2, +cubeSize/2, -cubeSize/2);
face.setVertex(3, +cubeSize/2, +cubeSize/2, +cubeSize/2);
for (int i = 0; i < circleRes; i++) {
float angle = TWO_PI * i / circleRes;
float x = +cubeSize/2;
float y = circleRad * sin(angle);
float z = circleRad * cos(angle);
face.setVertex(4 + i, x, y, z);
}
// Left face
face = cube.getChild(3);
face.setVertex(0, -cubeSize/2, -cubeSize/2, -cubeSize/2);
face.setVertex(1, -cubeSize/2, -cubeSize/2, +cubeSize/2);
face.setVertex(2, -cubeSize/2, +cubeSize/2, +cubeSize/2);
face.setVertex(3, -cubeSize/2, +cubeSize/2, -cubeSize/2);
for (int i = 0; i < circleRes; i++) {
float angle = TWO_PI * i / circleRes;
float x = -cubeSize/2;
float y = circleRad * sin(angle);
float z = circleRad * cos(angle);
face.setVertex(4 + i, x, y, z);
}
// Top face
face = cube.getChild(4);
face.setVertex(0, -cubeSize/2, +cubeSize/2, +cubeSize/2);
face.setVertex(1, +cubeSize/2, +cubeSize/2, +cubeSize/2);
face.setVertex(2, +cubeSize/2, +cubeSize/2, -cubeSize/2);
face.setVertex(3, -cubeSize/2, +cubeSize/2, -cubeSize/2);
for (int i = 0; i < circleRes; i++) {
float angle = TWO_PI * i / circleRes;
float x = circleRad * sin(angle);
float y = +cubeSize/2;
float z = circleRad * cos(angle);
face.setVertex(4 + i, x, y, z);
}
// Bottom face
face = cube.getChild(5);
face.setVertex(0, +cubeSize/2, -cubeSize/2, +cubeSize/2);
face.setVertex(1, -cubeSize/2, -cubeSize/2, +cubeSize/2);
face.setVertex(2, -cubeSize/2, -cubeSize/2, -cubeSize/2);
face.setVertex(3, +cubeSize/2, -cubeSize/2, -cubeSize/2);
for (int i = 0; i < circleRes; i++) {
float angle = TWO_PI * i / circleRes;
float x = circleRad * sin(angle);
float y = -cubeSize/2;
float z = circleRad * cos(angle);
face.setVertex(4 + i, x, y, z);
}
}

View File

@@ -0,0 +1,256 @@
class Gesture {
float damp = 5.0;
float dampInv = 1.0 / damp;
float damp1 = damp - 1;
int w;
int h;
int capacity;
Vec3f path[];
int crosses[];
Polygon polygons[];
int nPoints;
int nPolys;
float jumpDx, jumpDy;
boolean exists;
float INIT_TH = 14;
float thickness = INIT_TH;
Gesture(int mw, int mh) {
w = mw;
h = mh;
capacity = 600;
path = new Vec3f[capacity];
polygons = new Polygon[capacity];
crosses = new int[capacity];
for (int i=0;i<capacity;i++) {
polygons[i] = new Polygon();
polygons[i].npoints = 4;
path[i] = new Vec3f();
crosses[i] = 0;
}
nPoints = 0;
nPolys = 0;
exists = false;
jumpDx = 0;
jumpDy = 0;
}
void clear() {
nPoints = 0;
exists = false;
thickness = INIT_TH;
}
void clearPolys() {
nPolys = 0;
}
void addPoint(float x, float y) {
if (nPoints >= capacity) {
// there are all sorts of possible solutions here,
// but for abject simplicity, I don't do anything.
}
else {
float v = distToLast(x, y);
float p = getPressureFromVelocity(v);
path[nPoints++].set(x,y,p);
if (nPoints > 1) {
exists = true;
jumpDx = path[nPoints-1].x - path[0].x;
jumpDy = path[nPoints-1].y - path[0].y;
}
}
}
float getPressureFromVelocity(float v) {
final float scale = 18;
final float minP = 0.02;
final float oldP = (nPoints > 0) ? path[nPoints-1].p : 0;
return ((minP + max(0, 1.0 - v/scale)) + (damp1*oldP))*dampInv;
}
void setPressures() {
// pressures vary from 0...1
float pressure;
Vec3f tmp;
float t = 0;
float u = 1.0 / (nPoints - 1)*TWO_PI;
for (int i = 0; i < nPoints; i++) {
pressure = sqrt((1.0 - cos(t))*0.5);
path[i].p = pressure;
t += u;
}
}
float distToLast(float ix, float iy) {
if (nPoints > 0) {
Vec3f v = path[nPoints-1];
float dx = v.x - ix;
float dy = v.y - iy;
return mag(dx, dy);
}
else {
return 30;
}
}
void compile() {
// compute the polygons from the path of Vec3f's
if (exists) {
clearPolys();
Vec3f p0, p1, p2;
float radius0, radius1;
float ax, bx, cx, dx;
float ay, by, cy, dy;
int axi, bxi, cxi, dxi, axip, axid;
int ayi, byi, cyi, dyi, ayip, ayid;
float p1x, p1y;
float dx01, dy01, hp01, si01, co01;
float dx02, dy02, hp02, si02, co02;
float dx13, dy13, hp13, si13, co13;
float taper = 1.0;
int nPathPoints = nPoints - 1;
int lastPolyIndex = nPathPoints - 1;
float npm1finv = 1.0 / max(1, nPathPoints - 1);
// handle the first point
p0 = path[0];
p1 = path[1];
radius0 = p0.p * thickness;
dx01 = p1.x - p0.x;
dy01 = p1.y - p0.y;
hp01 = sqrt(dx01*dx01 + dy01*dy01);
if (hp01 == 0) {
hp02 = 0.0001;
}
co01 = radius0 * dx01 / hp01;
si01 = radius0 * dy01 / hp01;
ax = p0.x - si01;
ay = p0.y + co01;
bx = p0.x + si01;
by = p0.y - co01;
int xpts[];
int ypts[];
int LC = 20;
int RC = w-LC;
int TC = 20;
int BC = h-TC;
float mint = 0.618;
float tapow = 0.4;
// handle the middle points
int i = 1;
Polygon apoly;
for (i = 1; i < nPathPoints; i++) {
taper = pow((lastPolyIndex-i)*npm1finv,tapow);
p0 = path[i-1];
p1 = path[i ];
p2 = path[i+1];
p1x = p1.x;
p1y = p1.y;
radius1 = Math.max(mint,taper*p1.p*thickness);
// assumes all segments are roughly the same length...
dx02 = p2.x - p0.x;
dy02 = p2.y - p0.y;
hp02 = (float) Math.sqrt(dx02*dx02 + dy02*dy02);
if (hp02 != 0) {
hp02 = radius1/hp02;
}
co02 = dx02 * hp02;
si02 = dy02 * hp02;
// translate the integer coordinates to the viewing rectangle
axi = axip = (int)ax;
ayi = ayip = (int)ay;
axi=(axi<0)?(w-((-axi)%w)):axi%w;
axid = axi-axip;
ayi=(ayi<0)?(h-((-ayi)%h)):ayi%h;
ayid = ayi-ayip;
// set the vertices of the polygon
apoly = polygons[nPolys++];
xpts = apoly.xpoints;
ypts = apoly.ypoints;
xpts[0] = axi = axid + axip;
xpts[1] = bxi = axid + (int) bx;
xpts[2] = cxi = axid + (int)(cx = p1x + si02);
xpts[3] = dxi = axid + (int)(dx = p1x - si02);
ypts[0] = ayi = ayid + ayip;
ypts[1] = byi = ayid + (int) by;
ypts[2] = cyi = ayid + (int)(cy = p1y - co02);
ypts[3] = dyi = ayid + (int)(dy = p1y + co02);
// keep a record of where we cross the edge of the screen
crosses[i] = 0;
if ((axi<=LC)||(bxi<=LC)||(cxi<=LC)||(dxi<=LC)) {
crosses[i]|=1;
}
if ((axi>=RC)||(bxi>=RC)||(cxi>=RC)||(dxi>=RC)) {
crosses[i]|=2;
}
if ((ayi<=TC)||(byi<=TC)||(cyi<=TC)||(dyi<=TC)) {
crosses[i]|=4;
}
if ((ayi>=BC)||(byi>=BC)||(cyi>=BC)||(dyi>=BC)) {
crosses[i]|=8;
}
//swap data for next time
ax = dx;
ay = dy;
bx = cx;
by = cy;
}
// handle the last point
p2 = path[nPathPoints];
apoly = polygons[nPolys++];
xpts = apoly.xpoints;
ypts = apoly.ypoints;
xpts[0] = (int)ax;
xpts[1] = (int)bx;
xpts[2] = (int)(p2.x);
xpts[3] = (int)(p2.x);
ypts[0] = (int)ay;
ypts[1] = (int)by;
ypts[2] = (int)(p2.y);
ypts[3] = (int)(p2.y);
}
}
void smooth() {
// average neighboring points
final float weight = 18;
final float scale = 1.0 / (weight + 2);
int nPointsMinusTwo = nPoints - 2;
Vec3f lower, upper, center;
for (int i = 1; i < nPointsMinusTwo; i++) {
lower = path[i-1];
center = path[i];
upper = path[i+1];
center.x = (lower.x + weight*center.x + upper.x)*scale;
center.y = (lower.y + weight*center.y + upper.y)*scale;
}
}
}

View File

@@ -0,0 +1,19 @@
class Vec3f {
float x;
float y;
float p; // Pressure
Vec3f() {
set(0, 0, 0);
}
Vec3f(float ix, float iy, float ip) {
set(ix, iy, ip);
}
void set(float ix, float iy, float ip) {
x = ix;
y = iy;
p = ip;
}
}

View File

@@ -0,0 +1,189 @@
/**
* Yellowtail
* by Golan Levin (www.flong.com).
*
* Click, drag, and release to create a kinetic gesture.
*
* Yellowtail (1998-2000) is an interactive software system for the gestural
* creation and performance of real-time abstract animation. Yellowtail repeats
* a user's strokes end-over-end, enabling simultaneous specification of a
* line's shape and quality of movement. Each line repeats according to its
* own period, producing an ever-changing and responsive display of lively,
* worm-like textures.
*/
import java.awt.Polygon;
Gesture gestureArray[];
final int nGestures = 36; // Number of gestures
final int minMove = 3; // Minimum travel for a new point
int currentGestureID;
Polygon tempP;
int tmpXp[];
int tmpYp[];
void setup() {
size(1024, 768, P2D);
background(0, 0, 0);
noStroke();
currentGestureID = -1;
gestureArray = new Gesture[nGestures];
for (int i = 0; i < nGestures; i++) {
gestureArray[i] = new Gesture(width, height);
}
clearGestures();
}
void draw() {
background(0);
updateGeometry();
fill(255, 255, 245);
for (int i = 0; i < nGestures; i++) {
renderGesture(gestureArray[i], width, height);
}
}
void mousePressed() {
currentGestureID = (currentGestureID+1) % nGestures;
Gesture G = gestureArray[currentGestureID];
G.clear();
G.clearPolys();
G.addPoint(mouseX, mouseY);
}
void mouseDragged() {
if (currentGestureID >= 0) {
Gesture G = gestureArray[currentGestureID];
if (G.distToLast(mouseX, mouseY) > minMove) {
G.addPoint(mouseX, mouseY);
G.smooth();
G.compile();
}
}
}
void keyPressed() {
if (key == '+' || key == '=') {
if (currentGestureID >= 0) {
float th = gestureArray[currentGestureID].thickness;
gestureArray[currentGestureID].thickness = min(96, th+1);
gestureArray[currentGestureID].compile();
}
} else if (key == '-') {
if (currentGestureID >= 0) {
float th = gestureArray[currentGestureID].thickness;
gestureArray[currentGestureID].thickness = max(2, th-1);
gestureArray[currentGestureID].compile();
}
} else if (key == ' ') {
clearGestures();
}
}
void renderGesture(Gesture gesture, int w, int h) {
if (gesture.exists) {
if (gesture.nPolys > 0) {
Polygon polygons[] = gesture.polygons;
int crosses[] = gesture.crosses;
int xpts[];
int ypts[];
Polygon p;
int cr;
beginShape(QUADS);
int gnp = gesture.nPolys;
for (int i=0; i<gnp; i++) {
p = polygons[i];
xpts = p.xpoints;
ypts = p.ypoints;
vertex(xpts[0], ypts[0]);
vertex(xpts[1], ypts[1]);
vertex(xpts[2], ypts[2]);
vertex(xpts[3], ypts[3]);
if ((cr = crosses[i]) > 0) {
if ((cr & 3)>0) {
vertex(xpts[0]+w, ypts[0]);
vertex(xpts[1]+w, ypts[1]);
vertex(xpts[2]+w, ypts[2]);
vertex(xpts[3]+w, ypts[3]);
vertex(xpts[0]-w, ypts[0]);
vertex(xpts[1]-w, ypts[1]);
vertex(xpts[2]-w, ypts[2]);
vertex(xpts[3]-w, ypts[3]);
}
if ((cr & 12)>0) {
vertex(xpts[0], ypts[0]+h);
vertex(xpts[1], ypts[1]+h);
vertex(xpts[2], ypts[2]+h);
vertex(xpts[3], ypts[3]+h);
vertex(xpts[0], ypts[0]-h);
vertex(xpts[1], ypts[1]-h);
vertex(xpts[2], ypts[2]-h);
vertex(xpts[3], ypts[3]-h);
}
// I have knowingly retained the small flaw of not
// completely dealing with the corner conditions
// (the case in which both of the above are true).
}
}
endShape();
}
}
}
void updateGeometry() {
Gesture J;
for (int g=0; g<nGestures; g++) {
if ((J=gestureArray[g]).exists) {
if (g!=currentGestureID) {
advanceGesture(J);
} else if (!mousePressed) {
advanceGesture(J);
}
}
}
}
void advanceGesture(Gesture gesture) {
// Move a Gesture one step
if (gesture.exists) { // check
int nPts = gesture.nPoints;
int nPts1 = nPts-1;
Vec3f path[];
float jx = gesture.jumpDx;
float jy = gesture.jumpDy;
if (nPts > 0) {
path = gesture.path;
for (int i = nPts1; i > 0; i--) {
path[i].x = path[i-1].x;
path[i].y = path[i-1].y;
}
path[0].x = path[nPts1].x - jx;
path[0].y = path[nPts1].y - jy;
gesture.compile();
}
}
}
void clearGestures() {
for (int i = 0; i < nGestures; i++) {
gestureArray[i].clear();
}
}

View File

@@ -0,0 +1,28 @@
/**
* Move Eye.
* by Simon Greenwold.
*
* The camera lifts up (controlled by mouseY) while looking at the same point.
*/
void setup() {
size(640, 360, P3D);
fill(204);
}
void draw() {
lights();
background(0);
// Change height of the camera with mouseY
camera(30.0, mouseY, 220.0, // eyeX, eyeY, eyeZ
0.0, 0.0, 0.0, // centerX, centerY, centerZ
0.0, 1.0, 0.0); // upX, upY, upZ
noStroke();
box(90);
stroke(255);
line(-100, 0, 0, 100, 0, 0);
line(0, -100, 0, 0, 100, 0);
line(0, 0, -100, 0, 0, 100);
}

View File

@@ -0,0 +1,115 @@
/**
* Near and Far example
*
* This example shows the effect of setting the near and far values
* in the ortho() and perspective() functions. Both values are always
* measured from the camera eye position and along the eye-center
* vector.
*
* The spacebar switches between perspective and orthographic
* projections, to set the near value press 'n' and 'f' for far.
* A mouse click switches the near/far setting on and off.
* 'r' enables/disables rotating the object with the mouse.
*/
boolean usingPerspective = true;
boolean settingFar = true;
boolean settingEnabled = true;
boolean rotating = false;
float cameraFOV;
float cameraZ;
float cameraMaxFar;
float cameraNear;
float cameraFar;
float angleX = -PI/6;
float angleY = PI/3;
void setup() {
size(640, 360, P3D);
cameraFOV = PI/3.0;
cameraZ = (height/2.0) / tan(cameraFOV/2.0);
cameraMaxFar = cameraZ * 2.0;
cameraNear = cameraZ / 2.0;
cameraFar = cameraZ * 2.0;
}
void draw() {
background(0);
lights();
if (settingEnabled) {
if (settingFar) {
float minx = map(cameraNear, 0, cameraMaxFar, 0, width);
cameraFar = map(mouseX, minx, width, cameraNear, cameraMaxFar);
} else {
float maxx = map(cameraFar, 0, cameraMaxFar, 0, width);
cameraNear = map(mouseX, 0, maxx, 0, cameraFar);
}
}
if (usingPerspective) {
perspective(cameraFOV, float(width)/float(height), cameraNear, cameraFar);
} else {
ortho(-width/2, width/2, -height/2, height/2, cameraNear, cameraFar);
}
pushMatrix();
translate(width/2, height/2, 0);
if (rotating) {
angleX = map(mouseX, 0, width, -PI, PI);
angleY = map(mouseY, 0, width, -PI, PI);
}
rotateX(angleX);
rotateY(angleY);
stroke(50);
fill(204);
box(160);
popMatrix();
// Drawing visual clues for the near and far values.
perspective(); // Restoring to the default perspective matrix.
noStroke();
float near = map(cameraNear, 0, cameraMaxFar, 0, width);
float far = map(cameraFar, 0, cameraMaxFar, 0, width);
fill(204);
rect(0, 0, near, 20);
rect(far, 0, width - far, 20);
if (near <= far) {
fill(160);
} else {
fill(200, 50, 50);
}
rect(near, 0, far - near, 20);
}
void keyPressed() {
if (key == ' ') {
usingPerspective = !usingPerspective;
} else if (key == 'f' || key == 'F') {
settingFar = true;
} else if (key == 'n' || key == 'N') {
settingFar = false;
} else if (key == 'r' || key == 'R') {
if (rotating) {
rotating = false;
} else {
rotating = true;
settingEnabled = false;
}
}
}
void mousePressed() {
if (!rotating) {
settingEnabled = !settingEnabled;
}
}

View File

@@ -0,0 +1,41 @@
/**
* Ortho vs Perspective.
*
* Click to see the difference between orthographic projection
* and perspective projection as applied to a simple box.
* The ortho() function sets an orthographic projection and
* defines a parallel clipping volume. All objects with the
* same dimension appear the same size, regardless of whether
* they are near or far from the camera. The parameters to this
* function specify the clipping volume where left and right
* are the minimum and maximum x values, top and bottom are the
* minimum and maximum y values, and near and far are the minimum
* and maximum z values.
*/
void setup()
{
size(640, 360, P3D);
noStroke();
fill(204);
}
void draw()
{
background(0);
lights();
if(mousePressed) {
float fov = PI/3.0;
float cameraZ = (height/2.0) / tan(fov/2.0);
perspective(fov, float(width)/float(height),
cameraZ/2.0, cameraZ*2.0);
} else {
ortho(-width/2, width/2, -height/2, height/2, 0, 400);
}
translate(width/2, height/2, 0);
rotateX(-PI/6);
rotateY(PI/3);
box(160);
}

View File

@@ -0,0 +1,41 @@
/**
* Perspective.
*
* Move the mouse left and right to change the field of view (fov).
* Click to modify the aspect ratio. The perspective() function
* sets a perspective projection applying foreshortening, making
* distant objects appear smaller than closer ones. The parameters
* define a viewing volume with the shape of truncated pyramid.
* Objects near to the front of the volume appear their actual size,
* while farther objects appear smaller. This projection simulates
* the perspective of the world more accurately than orthographic projection.
* The version of perspective without parameters sets the default
* perspective and the version with four parameters allows the programmer
* to set the area precisely.
*/
void setup() {
size(640, 360, P3D);
noStroke();
}
void draw() {
lights();
background(204);
float cameraY = height/2.0;
float fov = mouseX/float(width) * PI/2;
float cameraZ = cameraY / tan(fov / 2.0);
float aspect = float(width)/float(height);
if (mousePressed) {
aspect = aspect / 2.0;
}
perspective(fov, aspect, cameraZ/10.0, cameraZ*10.0);
translate(width/2+30, height/2, 0);
rotateX(-PI/6);
rotateY(PI/3 + mouseY/float(height) * PI);
box(45);
translate(0, 0, -50);
box(30);
}

View File

@@ -0,0 +1,58 @@
/**
* Brick Tower
* by Ira Greenberg.
*
* 3D castle tower constructed out of individual bricks.
* Uses the PVector and Cube classes.
*/
float bricksPerLayer = 16.0;
float brickLayers = 18.0;
Cube brick;
float brickWidth = 60, brickHeight = 25, brickDepth = 25;
float radius = 175.0;
float angle = 0;
void setup(){
size(640, 360, P3D);
brick = new Cube(brickWidth, brickHeight, brickDepth);
}
void draw(){
background(0);
float tempX = 0, tempY = 0, tempZ = 0;
fill(182, 62, 29);
noStroke();
// Add basic light setup
lights();
translate(width/2, height*1.2, -380);
// Tip tower to see inside
rotateX(radians(-45));
// Slowly rotate tower
rotateY(frameCount * PI/600);
for (int i = 0; i < brickLayers; i++){
// Increment rows
tempY-=brickHeight;
// Alternate brick seams
angle = 360.0 / bricksPerLayer * i/2;
for (int j = 0; j < bricksPerLayer; j++){
tempZ = cos(radians(angle))*radius;
tempX = sin(radians(angle))*radius;
pushMatrix();
translate(tempX, tempY, tempZ);
rotateY(radians(angle));
// Add crenelation
if (i==brickLayers-1){
if (j%2 == 0){
brick.create();
}
}
// Create main tower
else {
brick.create();
}
popMatrix();
angle += 360.0/bricksPerLayer;
}
}
}

View File

@@ -0,0 +1,68 @@
class Cube {
PVector[] vertices = new PVector[24];
PVector[] normals = new PVector[6];
float w, h, d;
Cube(){ }
Cube(float w, float h, float d){
this.w = w;
this.h = h;
this.d = d;
// Cube composed of 6 quads
// Front
normals[0] = new PVector(0, 0, 1);
vertices[0] = new PVector(-w/2, -h/2, d/2);
vertices[1] = new PVector(w/2, -h/2, d/2);
vertices[2] = new PVector(w/2, h/2, d/2);
vertices[3] = new PVector(-w/2, h/2, d/2);
// Left
normals[1] = new PVector(-1, 0, 0);
vertices[4] = new PVector(-w/2, -h/2, d/2);
vertices[5] = new PVector(-w/2, -h/2, -d/2);
vertices[6] = new PVector(-w/2, h/2, -d/2);
vertices[7] = new PVector(-w/2, h/2, d/2);
// Right
normals[2] = new PVector(1, 0, 0);
vertices[8] = new PVector(w/2, -h/2, d/2);
vertices[9] = new PVector(w/2, -h/2, -d/2);
vertices[10] = new PVector(w/2, h/2, -d/2);
vertices[11] = new PVector(w/2, h/2, d/2);
// Back
normals[3] = new PVector(0, 0, -1);
vertices[12] = new PVector(-w/2, -h/2, -d/2);
vertices[13] = new PVector(w/2, -h/2, -d/2);
vertices[14] = new PVector(w/2, h/2, -d/2);
vertices[15] = new PVector(-w/2, h/2, -d/2);
// Top
normals[4] = new PVector(0, 1, 0);
vertices[16] = new PVector(-w/2, -h/2, d/2);
vertices[17] = new PVector(-w/2, -h/2, -d/2);
vertices[18] = new PVector(w/2, -h/2, -d/2);
vertices[19] = new PVector(w/2, -h/2, d/2);
// Bottom
normals[5] = new PVector(0, 1, 0);
vertices[20] = new PVector(-w/2, h/2, d/2);
vertices[21] = new PVector(-w/2, h/2, -d/2);
vertices[22] = new PVector(w/2, h/2, -d/2);
vertices[23] = new PVector(w/2, h/2, d/2);
}
void create(){
for (int i=0; i<6; i++){
beginShape(QUADS);
normal(normals[i].x, normals[i].y, normals[i].z);
for (int j = 0; j < 4; j++){
vertex(vertices[j+4*i].x, vertices[j+4*i].y, vertices[j+4*i].z);
}
endShape();
}
}
}

View File

@@ -0,0 +1,48 @@
/**
* Cubic Grid
* by Ira Greenberg.
*
* 3D translucent colored grid uses nested pushMatrix()
* and popMatrix() functions.
*/
float boxSize = 40;
float margin = boxSize*2;
float depth = 400;
color boxFill;
void setup() {
size(640, 360, P3D);
noStroke();
hint(DISABLE_DEPTH_TEST);
}
void draw() {
background(255);
// Center and spin grid
translate(width/2, height/2, -depth);
rotateY(frameCount * 0.01);
rotateX(frameCount * 0.01);
// Build grid using multiple translations
for (float i =- depth/2+margin; i <= depth/2-margin; i += boxSize){
pushMatrix();
for (float j =- height+margin; j <= height-margin; j += boxSize){
pushMatrix();
for (float k =- width+margin; k <= width-margin; k += boxSize){
// Base fill color on counter values, abs function
// ensures values stay within legal range
boxFill = color(abs(i), abs(j), abs(k), 50);
pushMatrix();
translate(k, j, i);
fill(boxFill);
box(boxSize, boxSize, boxSize);
popMatrix();
}
popMatrix();
}
popMatrix();
}
}

View File

@@ -0,0 +1,162 @@
/**
* Geometry
* by Marius Watz.
*
* Using sin/cos lookup tables, blends colors, and draws a series of
* rotating arcs on the screen.
*/
// Trig lookup tables borrowed from Toxi; cryptic but effective.
float sinLUT[];
float cosLUT[];
float SINCOS_PRECISION=1.0;
int SINCOS_LENGTH= int((360.0/SINCOS_PRECISION));
// System data
boolean dosave=false;
int num;
float pt[];
int style[];
void setup() {
size(1024, 768, P3D);
background(255);
// Fill the tables
sinLUT=new float[SINCOS_LENGTH];
cosLUT=new float[SINCOS_LENGTH];
for (int i = 0; i < SINCOS_LENGTH; i++) {
sinLUT[i]= (float)Math.sin(i*DEG_TO_RAD*SINCOS_PRECISION);
cosLUT[i]= (float)Math.cos(i*DEG_TO_RAD*SINCOS_PRECISION);
}
num = 150;
pt = new float[6*num]; // rotx, roty, deg, rad, w, speed
style = new int[2*num]; // color, render style
// Set up arc shapes
int index=0;
float prob;
for (int i=0; i<num; i++) {
pt[index++] = random(PI*2); // Random X axis rotation
pt[index++] = random(PI*2); // Random Y axis rotation
pt[index++] = random(60,80); // Short to quarter-circle arcs
if(random(100)>90) pt[index]=(int)random(8,27)*10;
pt[index++] = int(random(2,50)*5); // Radius. Space them out nicely
pt[index++] = random(4,32); // Width of band
if(random(100)>90) pt[index]=random(40,60); // Width of band
pt[index++] = radians(random(5,30))/5; // Speed of rotation
// get colors
prob = random(100);
if(prob<30) style[i*2]=colorBlended(random(1), 255,0,100, 255,0,0, 210);
else if(prob<70) style[i*2]=colorBlended(random(1), 0,153,255, 170,225,255, 210);
else if(prob<90) style[i*2]=colorBlended(random(1), 200,255,0, 150,255,0, 210);
else style[i*2]=color(255,255,255, 220);
if(prob<50) style[i*2]=colorBlended(random(1), 200,255,0, 50,120,0, 210);
else if(prob<90) style[i*2]=colorBlended(random(1), 255,100,0, 255,255,0, 210);
else style[i*2]=color(255,255,255, 220);
style[i*2+1]=(int)(random(100))%3;
}
}
void draw() {
background(0);
int index=0;
translate(width/2, height/2, 0);
rotateX(PI/6);
rotateY(PI/6);
for (int i = 0; i < num; i++) {
pushMatrix();
rotateX(pt[index++]);
rotateY(pt[index++]);
if(style[i*2+1]==0) {
stroke(style[i*2]);
noFill();
strokeWeight(1);
arcLine(0,0, pt[index++],pt[index++],pt[index++]);
}
else if(style[i*2+1]==1) {
fill(style[i*2]);
noStroke();
arcLineBars(0,0, pt[index++],pt[index++],pt[index++]);
}
else {
fill(style[i*2]);
noStroke();
arc(0,0, pt[index++],pt[index++],pt[index++]);
}
// increase rotation
pt[index-5]+=pt[index]/10;
pt[index-4]+=pt[index++]/20;
popMatrix();
}
}
// Get blend of two colors
int colorBlended(float fract,
float r, float g, float b,
float r2, float g2, float b2, float a) {
r2 = (r2 - r);
g2 = (g2 - g);
b2 = (b2 - b);
return color(r + r2 * fract, g + g2 * fract, b + b2 * fract, a);
}
// Draw arc line
void arcLine(float x,float y,float deg,float rad,float w) {
int a=(int)(min (deg/SINCOS_PRECISION,SINCOS_LENGTH-1));
int numlines=(int)(w/2);
for (int j=0; j<numlines; j++) {
beginShape();
for (int i=0; i<a; i++) {
vertex(cosLUT[i]*rad+x,sinLUT[i]*rad+y);
}
endShape();
rad += 2;
}
}
// Draw arc line with bars
void arcLineBars(float x,float y,float deg,float rad,float w) {
int a = int((min (deg/SINCOS_PRECISION,SINCOS_LENGTH-1)));
a /= 4;
beginShape(QUADS);
for (int i=0; i<a; i+=4) {
vertex(cosLUT[i]*(rad)+x,sinLUT[i]*(rad)+y);
vertex(cosLUT[i]*(rad+w)+x,sinLUT[i]*(rad+w)+y);
vertex(cosLUT[i+2]*(rad+w)+x,sinLUT[i+2]*(rad+w)+y);
vertex(cosLUT[i+2]*(rad)+x,sinLUT[i+2]*(rad)+y);
}
endShape();
}
// Draw solid arc
void arc(float x,float y,float deg,float rad,float w) {
int a = int(min (deg/SINCOS_PRECISION,SINCOS_LENGTH-1));
beginShape(QUAD_STRIP);
for (int i = 0; i < a; i++) {
vertex(cosLUT[i]*(rad)+x,sinLUT[i]*(rad)+y);
vertex(cosLUT[i]*(rad+w)+x,sinLUT[i]*(rad+w)+y);
}
endShape();
}

View File

@@ -0,0 +1,10 @@
class Dimension3D{
float w, h, d;
Dimension3D(float w, float h, float d){
this.w=w;
this.h=h;
this.d=d;
}
}

View File

@@ -0,0 +1,52 @@
/**
* I Like Icosahedra
* by Ira Greenberg.
*
* This example plots icosahedra. The Icosahdron is a regular
* polyhedron composed of twenty equalateral triangles.
*/
Icosahedron ico1;
Icosahedron ico2;
Icosahedron ico3;
void setup(){
size(640, 360, P3D);
ico1 = new Icosahedron(75);
ico2 = new Icosahedron(75);
ico3 = new Icosahedron(75);
}
void draw(){
background(0);
lights();
translate(width/2, height/2);
pushMatrix();
translate(-width/3.5, 0);
rotateX(frameCount*PI/185);
rotateY(frameCount*PI/-200);
stroke(170, 0, 0);
noFill();
ico1.create();
popMatrix();
pushMatrix();
rotateX(frameCount*PI/200);
rotateY(frameCount*PI/300);
stroke(150, 0, 180);
fill(170, 170, 0);
ico2.create();
popMatrix();
pushMatrix();
translate(width/3.5, 0);
rotateX(frameCount*PI/-200);
rotateY(frameCount*PI/200);
noStroke();
fill(0, 0, 185);
ico3.create();
popMatrix();
}

View File

@@ -0,0 +1,159 @@
class Icosahedron extends Shape3D{
// icosahedron
PVector topPoint;
PVector[] topPent = new PVector[5];
PVector bottomPoint;
PVector[] bottomPent = new PVector[5];
float angle = 0, radius = 150;
float triDist;
float triHt;
float a, b, c;
// constructor
Icosahedron(float radius){
this.radius = radius;
init();
}
Icosahedron(PVector v, float radius){
super(v);
this.radius = radius;
init();
}
// calculate geometry
void init(){
c = dist(cos(0)*radius, sin(0)*radius, cos(radians(72))*radius, sin(radians(72))*radius);
b = radius;
a = (float)(Math.sqrt(((c*c)-(b*b))));
triHt = (float)(Math.sqrt((c*c)-((c/2)*(c/2))));
for (int i=0; i<topPent.length; i++){
topPent[i] = new PVector(cos(angle)*radius, sin(angle)*radius, triHt/2.0f);
angle+=radians(72);
}
topPoint = new PVector(0, 0, triHt/2.0f+a);
angle = 72.0f/2.0f;
for (int i=0; i<topPent.length; i++){
bottomPent[i] = new PVector(cos(angle)*radius, sin(angle)*radius, -triHt/2.0f);
angle+=radians(72);
}
bottomPoint = new PVector(0, 0, -(triHt/2.0f+a));
}
// draws icosahedron
void create(){
for (int i=0; i<topPent.length; i++){
// icosahedron top
beginShape();
if (i<topPent.length-1){
vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
vertex(x+topPoint.x, y+topPoint.y, z+topPoint.z);
vertex(x+topPent[i+1].x, y+topPent[i+1].y, z+topPent[i+1].z);
}
else {
vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
vertex(x+topPoint.x, y+topPoint.y, z+topPoint.z);
vertex(x+topPent[0].x, y+topPent[0].y, z+topPent[0].z);
}
endShape(CLOSE);
// icosahedron bottom
beginShape();
if (i<bottomPent.length-1){
vertex(x+bottomPent[i].x, y+bottomPent[i].y, z+bottomPent[i].z);
vertex(x+bottomPoint.x, y+bottomPoint.y, z+bottomPoint.z);
vertex(x+bottomPent[i+1].x, y+bottomPent[i+1].y, z+bottomPent[i+1].z);
}
else {
vertex(x+bottomPent[i].x, y+bottomPent[i].y, z+bottomPent[i].z);
vertex(x+bottomPoint.x, y+bottomPoint.y, z+bottomPoint.z);
vertex(x+bottomPent[0].x, y+bottomPent[0].y, z+bottomPent[0].z);
}
endShape(CLOSE);
}
// icosahedron body
for (int i=0; i<topPent.length; i++){
if (i<topPent.length-2){
beginShape();
vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
vertex(x+bottomPent[i+1].x, y+bottomPent[i+1].y, z+bottomPent[i+1].z);
vertex(x+bottomPent[i+2].x, y+bottomPent[i+2].y, z+bottomPent[i+2].z);
endShape(CLOSE);
beginShape();
vertex(x+bottomPent[i+2].x, y+bottomPent[i+2].y, z+bottomPent[i+2].z);
vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
vertex(x+topPent[i+1].x, y+topPent[i+1].y, z+topPent[i+1].z);
endShape(CLOSE);
}
else if (i==topPent.length-2){
beginShape();
vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
vertex(x+bottomPent[i+1].x, y+bottomPent[i+1].y, z+bottomPent[i+1].z);
vertex(x+bottomPent[0].x, y+bottomPent[0].y, z+bottomPent[0].z);
endShape(CLOSE);
beginShape();
vertex(x+bottomPent[0].x, y+bottomPent[0].y, z+bottomPent[0].z);
vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
vertex(x+topPent[i+1].x, y+topPent[i+1].y, z+topPent[i+1].z);
endShape(CLOSE);
}
else if (i==topPent.length-1){
beginShape();
vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
vertex(x+bottomPent[0].x, y+bottomPent[0].y, z+bottomPent[0].z);
vertex(x+bottomPent[1].x, y+bottomPent[1].y, z+bottomPent[1].z);
endShape(CLOSE);
beginShape();
vertex(x+bottomPent[1].x, y+bottomPent[1].y, z+bottomPent[1].z);
vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
vertex(x+topPent[0].x, y+topPent[0].y, z+topPent[0].z);
endShape(CLOSE);
}
}
}
// overrided methods fom Shape3D
void rotZ(float theta){
float tx=0, ty=0, tz=0;
// top point
tx = cos(theta)*topPoint.x+sin(theta)*topPoint.y;
ty = sin(theta)*topPoint.x-cos(theta)*topPoint.y;
topPoint.x = tx;
topPoint.y = ty;
// bottom point
tx = cos(theta)*bottomPoint.x+sin(theta)*bottomPoint.y;
ty = sin(theta)*bottomPoint.x-cos(theta)*bottomPoint.y;
bottomPoint.x = tx;
bottomPoint.y = ty;
// top and bottom pentagons
for (int i=0; i<topPent.length; i++){
tx = cos(theta)*topPent[i].x+sin(theta)*topPent[i].y;
ty = sin(theta)*topPent[i].x-cos(theta)*topPent[i].y;
topPent[i].x = tx;
topPent[i].y = ty;
tx = cos(theta)*bottomPent[i].x+sin(theta)*bottomPent[i].y;
ty = sin(theta)*bottomPent[i].x-cos(theta)*bottomPent[i].y;
bottomPent[i].x = tx;
bottomPent[i].y = ty;
}
}
void rotX(float theta){
}
void rotY(float theta){
}
}

View File

@@ -0,0 +1,82 @@
abstract class Shape3D{
float x, y, z;
float w, h, d;
Shape3D(){
}
Shape3D(float x, float y, float z){
this.x = x;
this.y = y;
this.z = z;
}
Shape3D(PVector p){
x = p.x;
y = p.y;
z = p.z;
}
Shape3D(Dimension3D dim){
w = dim.w;
h = dim.h;
d = dim.d;
}
Shape3D(float x, float y, float z, float w, float h, float d){
this.x = x;
this.y = y;
this.z = z;
this.w = w;
this.h = h;
this.d = d;
}
Shape3D(float x, float y, float z, Dimension3D dim){
this.x = x;
this.y = y;
this.z = z;
w = dim.w;
h = dim.h;
d = dim.d;
}
Shape3D(PVector p, Dimension3D dim){
x = p.x;
y = p.y;
z = p.z;
w = dim.w;
h = dim.h;
d = dim.d;
}
void setLoc(PVector p){
x=p.x;
y=p.y;
z=p.z;
}
void setLoc(float x, float y, float z){
this.x=x;
this.y=y;
this.z=z;
}
// override if you need these
void rotX(float theta){
}
void rotY(float theta){
}
void rotZ(float theta){
}
// must be implemented in subclasses
abstract void init();
abstract void create();
}

View File

@@ -0,0 +1,30 @@
/**
* Primitives 3D.
*
* Placing mathematically 3D objects in synthetic space.
* The lights() method reveals their imagined dimension.
* The box() and sphere() functions each have one parameter
* which is used to specify their size. These shapes are
* positioned using the translate() function.
*/
size(640, 360, P3D);
background(0);
lights();
noStroke();
pushMatrix();
translate(130, height/2, 0);
rotateY(1.25);
rotateX(-0.4);
box(100);
popMatrix();
noFill();
stroke(255);
pushMatrix();
translate(500, height*0.35, -200);
sphere(280);
popMatrix();

View File

@@ -0,0 +1,74 @@
/**
* RGB Cube.
*
* The three primary colors of the additive color model are red, green, and blue.
* This RGB color cube displays smooth transitions between these colors.
*/
float xmag, ymag = 0;
float newXmag, newYmag = 0;
void setup()
{
size(640, 360, P3D);
noStroke();
colorMode(RGB, 1);
}
void draw()
{
background(0.5);
pushMatrix();
translate(width/2, height/2, -30);
newXmag = mouseX/float(width) * TWO_PI;
newYmag = mouseY/float(height) * TWO_PI;
float diff = xmag-newXmag;
if (abs(diff) > 0.01) { xmag -= diff/4.0; }
diff = ymag-newYmag;
if (abs(diff) > 0.01) { ymag -= diff/4.0; }
rotateX(-ymag);
rotateY(-xmag);
scale(90);
beginShape(QUADS);
fill(0, 1, 1); vertex(-1, 1, 1);
fill(1, 1, 1); vertex( 1, 1, 1);
fill(1, 0, 1); vertex( 1, -1, 1);
fill(0, 0, 1); vertex(-1, -1, 1);
fill(1, 1, 1); vertex( 1, 1, 1);
fill(1, 1, 0); vertex( 1, 1, -1);
fill(1, 0, 0); vertex( 1, -1, -1);
fill(1, 0, 1); vertex( 1, -1, 1);
fill(1, 1, 0); vertex( 1, 1, -1);
fill(0, 1, 0); vertex(-1, 1, -1);
fill(0, 0, 0); vertex(-1, -1, -1);
fill(1, 0, 0); vertex( 1, -1, -1);
fill(0, 1, 0); vertex(-1, 1, -1);
fill(0, 1, 1); vertex(-1, 1, 1);
fill(0, 0, 1); vertex(-1, -1, 1);
fill(0, 0, 0); vertex(-1, -1, -1);
fill(0, 1, 0); vertex(-1, 1, -1);
fill(1, 1, 0); vertex( 1, 1, -1);
fill(1, 1, 1); vertex( 1, 1, 1);
fill(0, 1, 1); vertex(-1, 1, 1);
fill(0, 0, 0); vertex(-1, -1, -1);
fill(1, 0, 0); vertex( 1, -1, -1);
fill(1, 0, 1); vertex( 1, -1, 1);
fill(0, 0, 1); vertex(-1, -1, 1);
endShape();
popMatrix();
}

View File

@@ -0,0 +1,162 @@
/**
* Legs class
* By Ira Greenberg <br />
* Processing for Flash Developers,
* Friends of ED, 2009
*/
class Legs {
// Instance properties with default values
float x = 0, y = 0, z = 0, w = 150, ht = 125;
color col = #77AA22;
// Advanced properties
float detailW = w/6.0;
float detailHt = ht/8.0;
float shoeBulge = detailHt*2.0;
float legGap = w/7.0;
// Dynamics properties
float velocity = .02, stepL, stepR, stepRate = random(10, 50);
float speedX = 1.0, speedZ, spring, damping = .5, theta;
// Default constructor
Legs() {
}
// Standard constructor
Legs(float x, float z, float w, float ht, color col) {
this.x = x;
this.z = z;
this.w = w;
this.ht = ht;
this.col = col;
fill(col);
detailW = w/6.0;
detailHt = ht/8.0;
shoeBulge = detailHt*2.0;
legGap = w/7.0;
speedX = random(-speedX, speedX);
}
// Advanced constructor
Legs(float x, float z, float w, float ht, color col, float detailW,
float detailHt, float shoeBulge, float legGap) {
this.x = x;
this.z = z;
this.w = w;
this.ht = ht;
this.col = col;
this.detailW = detailW;
this.detailHt = detailHt;
this.shoeBulge = shoeBulge;
this.legGap = legGap;
speedX = random(-speedX, speedX);
}
// Draw legs
void create() {
fill(col);
float footWidth = (w - legGap)/2;
beginShape();
vertex(x - w/2, y - ht, z);
vertex(x - w/2, y - ht + detailHt, z);
vertex(x - w/2 + detailW, y - ht + detailHt, z);
// left foot
vertex(x - w/2 + detailW, y + stepL, z);
curveVertex(x - w/2 + detailW, y + stepL, z);
curveVertex(x - w/2 + detailW, y + stepL, z);
curveVertex(x - w/2 + detailW - shoeBulge, y + detailHt/2 + stepL, z);
curveVertex(x - w/2, y + detailHt + stepL, z);
curveVertex(x - w/2, y + detailHt + stepL, z);
vertex(x - w/2 + footWidth, y + detailHt + stepL*.9, z);
// end left foot
vertex(x - w/2 + footWidth + legGap/2, y - ht + detailHt, z);
vertex(x - w/2 + footWidth + legGap/2, y - ht + detailHt, z);
// right foot
vertex(x - w/2 + footWidth + legGap, y + detailHt + stepR*.9, z);
vertex(x + w/2, y + detailHt + stepR, z);
curveVertex(x + w/2, y + detailHt + stepR, z);
curveVertex(x + w/2, y + detailHt + stepR, z);
curveVertex(x + w/2 - detailW + shoeBulge, y + detailHt/2 + stepR, z);
curveVertex(x + w/2 - detailW, y + stepR, z);
vertex(x + w/2 - detailW, y + stepR, z);
// end right foot
vertex(x + w/2 - detailW, y - ht + detailHt, z);
vertex(x + w/2, y - ht + detailHt, z);
vertex(x + w/2, y - ht, z);
endShape(CLOSE);
}
// Set advanced property values
void setDetails(float detailW, float detailHt, float shoeBulge, float legGap) {
this.detailW = detailW;
this.detailHt = detailHt;
this.shoeBulge = shoeBulge;
this.legGap = legGap;
}
// Make the legs step
void step(float stepRate) {
this.stepRate = stepRate;
spring = ht/2.0;
stepL = sin(theta)*spring;
stepR = cos(theta)*spring;
theta += radians(stepRate);
}
// Alternative overloaded step method
void step() {
spring = ht/2.0;
stepL = sin(theta)*spring;
stepR = cos(theta)*spring;
theta += radians(stepRate);
}
// Moves legs along x, y, z axes
void move() {
// Move legs along y-axis
y = stepR*damping;
// Move legs along x-axis and
// check for collision against frame edge
x += speedX;
if (screenX(x, y, z) > width) {
speedX *= -1;
}
if (screenX(x, y, z) < 0) {
speedX *= -1;
}
// Move legs along z-axis based on speed of stepping
// and check for collision against extremes
speedZ = (stepRate*velocity);
z += speedZ;
if (z > 400) {
z = 400;
velocity *= -1;
}
if (z < -100) {
z = -100;
velocity *= -1;
}
}
void setDynamics(float speedX, float spring, float damping) {
this.speedX = speedX;
this.spring = spring;
this.damping = damping;
}
}

View File

@@ -0,0 +1,45 @@
/**
* Run-Amuck
* By Ira Greenberg <br />
* Processing for Flash Developers,
* Friends of ED, 2009
*/
int count = 250;
Legs[] legs = new Legs[count];
void setup() {
size(640, 360, P3D);
noStroke();
for (int i = 0; i < legs.length; i++) {
legs[i] = new Legs(random(-10, 10), random(-50, 150), random(.5, 5),
random(.5, 5), color(random(255), random(255), random(255)));
}
}
void draw() {
background(0);
translate(width/2, height/2);
noStroke();
fill(35);
// Draw ground plane
beginShape();
vertex(-width*2, 0, -1000);
vertex(width*2, 0, -1000);
vertex(width/2, height/2, 400);
vertex(-width/2, height/2, 400);
endShape(CLOSE);
// Update and draw the legs
for (int i = 0; i < legs.length; i++) {
legs[i].create();
// Set foot step rate
legs[i].step(random(10, 50));
// Move legs along x, y, z axes
// z-movement dependent upon step rate
legs[i].move();
}
}

View File

@@ -0,0 +1,116 @@
/**
* Shape Transform
* by Ira Greenberg.
*
* Illustrates the geometric relationship
* between Cube, Pyramid, Cone and
* Cylinder 3D primitives.
*
* Instructions:<br />
* Up Arrow - increases points<br />
* Down Arrow - decreases points<br />
* 'p' key toggles between cube/pyramid<br />
*/
int pts = 4;
float angle = 0;
float radius = 99;
float cylinderLength = 95;
//vertices
PVector vertices[][];
boolean isPyramid = false;
float angleInc;
void setup(){
size(640, 360, P3D);
noStroke();
angleInc = PI/300.0;
}
void draw(){
background(170, 95, 95);
lights();
fill(255, 200, 200);
translate(width/2, height/2);
rotateX(frameCount * angleInc);
rotateY(frameCount * angleInc);
rotateZ(frameCount * angleInc);
// initialize vertex arrays
vertices = new PVector[2][pts+1];
// fill arrays
for (int i = 0; i < 2; i++){
angle = 0;
for(int j = 0; j <= pts; j++){
vertices[i][j] = new PVector();
if (isPyramid){
if (i==1){
vertices[i][j].x = 0;
vertices[i][j].y = 0;
}
else {
vertices[i][j].x = cos(radians(angle)) * radius;
vertices[i][j].y = sin(radians(angle)) * radius;
}
}
else {
vertices[i][j].x = cos(radians(angle)) * radius;
vertices[i][j].y = sin(radians(angle)) * radius;
}
vertices[i][j].z = cylinderLength;
// the .0 after the 360 is critical
angle += 360.0/pts;
}
cylinderLength *= -1;
}
// draw cylinder tube
beginShape(QUAD_STRIP);
for(int j = 0; j <= pts; j++){
vertex(vertices[0][j].x, vertices[0][j].y, vertices[0][j].z);
vertex(vertices[1][j].x, vertices[1][j].y, vertices[1][j].z);
}
endShape();
//draw cylinder ends
for (int i = 0; i < 2; i++){
beginShape();
for(int j = 0; j < pts; j++){
vertex(vertices[i][j].x, vertices[i][j].y, vertices[i][j].z);
}
endShape(CLOSE);
}
}
/*
up/down arrow keys control
polygon detail.
*/
void keyPressed(){
if(key == CODED) {
// pts
if (keyCode == UP) {
if (pts < 90){
pts++;
}
}
else if (keyCode == DOWN) {
if (pts > 4){
pts--;
}
}
}
if (key =='p'){
if (isPyramid){
isPyramid = false;
}
else {
isPyramid = true;
}
}
}

View File

@@ -0,0 +1,73 @@
class Cube {
// Properties
int w, h, d;
int shiftX, shiftY, shiftZ;
// Constructor
Cube(int w, int h, int d, int shiftX, int shiftY, int shiftZ){
this.w = w;
this.h = h;
this.d = d;
this.shiftX = shiftX;
this.shiftY = shiftY;
this.shiftZ = shiftZ;
}
// Main cube drawing method, which looks
// more confusing than it really is. It's
// just a bunch of rectangles drawn for
// each cube face
void drawCube(){
beginShape(QUADS);
// Front face
normal(0, 0, 1);
vertex(-w/2 + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, h + shiftY, -d/2 + shiftZ);
vertex(-w/2 + shiftX, h + shiftY, -d/2 + shiftZ);
// Back face
normal(0, 0, -1);
vertex(-w/2 + shiftX, -h/2 + shiftY, d + shiftZ);
vertex(w + shiftX, -h/2 + shiftY, d + shiftZ);
vertex(w + shiftX, h + shiftY, d + shiftZ);
vertex(-w/2 + shiftX, h + shiftY, d + shiftZ);
// Left face
normal(1, 0, 0);
vertex(-w/2 + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(-w/2 + shiftX, -h/2 + shiftY, d + shiftZ);
vertex(-w/2 + shiftX, h + shiftY, d + shiftZ);
vertex(-w/2 + shiftX, h + shiftY, -d/2 + shiftZ);
// Right face
normal(-1, 0, 0);
vertex(w + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, -h/2 + shiftY, d + shiftZ);
vertex(w + shiftX, h + shiftY, d + shiftZ);
vertex(w + shiftX, h + shiftY, -d/2 + shiftZ);
// Top face
normal(0, 1, 0);
vertex(-w/2 + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, -h/2 + shiftY, d + shiftZ);
vertex(-w/2 + shiftX, -h/2 + shiftY, d + shiftZ);
// Bottom face
normal(0, -1, 0);
vertex(-w/2 + shiftX, h + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, h + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, h + shiftY, d + shiftZ);
vertex(-w/2 + shiftX, h + shiftY, d + shiftZ);
endShape();
// Add some rotation to each box for pizazz.
rotateY(radians(1));
rotateX(radians(1));
rotateZ(radians(1));
}
}

View File

@@ -0,0 +1,63 @@
/**
* Space Junk
* by Ira Greenberg.
* Zoom suggestion
* by Danny Greenberg.
*
* Rotating cubes in space using a custom Cube class.
* Color controlled by light sources. Move the mouse left
* and right to zoom.
*/
// Used for oveall rotation
float ang;
// Cube count-lower/raise to test P3D/OPENGL performance
int limit = 500;
// Array for all cubes
Cube[]cubes = new Cube[limit];
void setup() {
size(1024, 768, P3D);
background(0);
noStroke();
// Instantiate cubes, passing in random vals for size and postion
for (int i = 0; i< cubes.length; i++){
cubes[i] = new Cube(int(random(-10, 10)), int(random(-10, 10)),
int(random(-10, 10)), int(random(-140, 140)), int(random(-140, 140)),
int(random(-140, 140)));
}
}
void draw(){
background(0);
fill(200);
// Set up some different colored lights
pointLight(51, 102, 255, 65, 60, 100);
pointLight(200, 40, 60, -65, -60, -150);
// Raise overall light in scene
ambientLight(70, 70, 10);
// Center geometry in display windwow.
// you can change 3rd argument ('0')
// to move block group closer(+)/further(-)
translate(width/2, height/2, -200 + mouseX * 0.65);
// Rotate around y and x axes
rotateY(radians(ang));
rotateX(radians(ang));
// Draw cubes
for (int i = 0; i < cubes.length; i++){
cubes[i].drawCube();
}
// Used in rotate function calls above
ang++;
}

View File

@@ -0,0 +1,62 @@
/**
* Textured Sphere
* by Mike 'Flux' Chang (cleaned up by Aaron Koblin).
* Based on code by Toxi.
*
* A 3D textured sphere with simple rotation control.
* Note: Controls will be inverted when sphere is upside down.
* Use an "arc ball" to deal with this appropriately.
*/
PShape globe;
PImage texmap;
int sDetail = 35; // Sphere detail setting
float rotationX = 0;
float rotationY = 0;
float velocityX = 0;
float velocityY = 0;
float globeRadius = 450;
float pushBack = -300;
float[] cx, cz, sphereX, sphereY, sphereZ;
float sinLUT[];
float cosLUT[];
float SINCOS_PRECISION = 0.5;
int SINCOS_LENGTH = int(360.0 / SINCOS_PRECISION);
void setup() {
size(1024, 768, P3D);
smooth();
sphereDetail(sDetail);
texmap = loadImage("world32k.jpg");
globe = createShape(SPHERE, globeRadius);
globe.noStroke();
globe.texture(texmap);
}
void draw() {
background(0);
renderGlobe();
}
void renderGlobe() {
translate(width/2.0, height/2.0, pushBack);
lights();
rotateX(radians(-rotationX));
rotateY(radians(-rotationY));
shape(globe);
rotationX += velocityX;
rotationY += velocityY;
velocityX *= 0.95;
velocityY *= 0.95;
// Implements mouse control (interaction will be inverse when sphere is upside down)
if(mousePressed){
velocityX += (mouseY-pmouseY) * 0.01;
velocityY -= (mouseX-pmouseX) * 0.01;
}
}

View File

@@ -0,0 +1,185 @@
// Strange "lights flashing" effect, particularly noticeable at low res (pts=10, segments=20)
// ...some problem with the automatic normal calculation maybe?
/**
* Interactive Toroid
* by Ira Greenberg.
*
* Illustrates the geometric relationship between Toroid, Sphere, and Helix
* 3D primitives, as well as lathing principal.
*
* Instructions: <br />
* UP arrow key pts++ <br />
* DOWN arrow key pts-- <br />
* LEFT arrow key segments-- <br />
* RIGHT arrow key segments++ <br />
* 'a' key toroid radius-- <br />
* 's' key toroid radius++ <br />
* 'z' key initial polygon radius-- <br />
* 'x' key initial polygon radius++ <br />
* 'w' key toggle wireframe/solid shading <br />
* 'h' key toggle sphere/helix <br />
*/
int pts = 40;
float angle = 0;
float radius = 60.0;
// lathe segments
int segments = 60;
float latheAngle = 0;
float latheRadius = 100.0;
//vertices
PVector vertices[], vertices2[];
// for shaded or wireframe rendering
boolean isWireFrame = false;
// for optional helix
boolean isHelix = false;
float helixOffset = 5.0;
void setup(){
size(640, 360, P3D);
}
void draw(){
background(50, 64, 42);
// basic lighting setup
lights();
// 2 rendering styles
// wireframe or solid
if (isWireFrame){
stroke(255, 255, 150);
noFill();
}
else {
noStroke();
fill(150, 195, 125);
}
//center and spin toroid
translate(width/2, height/2, -100);
rotateX(frameCount*PI/150);
rotateY(frameCount*PI/170);
rotateZ(frameCount*PI/90);
// initialize point arrays
vertices = new PVector[pts+1];
vertices2 = new PVector[pts+1];
// fill arrays
for(int i=0; i<=pts; i++){
vertices[i] = new PVector();
vertices2[i] = new PVector();
vertices[i].x = latheRadius + sin(radians(angle))*radius;
if (isHelix){
vertices[i].z = cos(radians(angle))*radius-(helixOffset*
segments)/2;
}
else{
vertices[i].z = cos(radians(angle))*radius;
}
angle+=360.0/pts;
}
// draw toroid
latheAngle = 0;
for(int i=0; i<=segments; i++){
beginShape(QUAD_STRIP);
for(int j=0; j<=pts; j++){
if (i>0){
vertex(vertices2[j].x, vertices2[j].y, vertices2[j].z);
}
vertices2[j].x = cos(radians(latheAngle))*vertices[j].x;
vertices2[j].y = sin(radians(latheAngle))*vertices[j].x;
vertices2[j].z = vertices[j].z;
// optional helix offset
if (isHelix){
vertices[j].z+=helixOffset;
}
vertex(vertices2[j].x, vertices2[j].y, vertices2[j].z);
}
// create extra rotation for helix
if (isHelix){
latheAngle+=720.0/segments;
}
else {
latheAngle+=360.0/segments;
}
endShape();
}
}
/*
left/right arrow keys control ellipse detail
up/down arrow keys control segment detail.
'a','s' keys control lathe radius
'z','x' keys control ellipse radius
'w' key toggles between wireframe and solid
'h' key toggles between toroid and helix
*/
void keyPressed(){
if(key == CODED) {
// pts
if (keyCode == UP) {
if (pts<40){
pts++;
}
}
else if (keyCode == DOWN) {
if (pts>3){
pts--;
}
}
// extrusion length
if (keyCode == LEFT) {
if (segments>3){
segments--;
}
}
else if (keyCode == RIGHT) {
if (segments<80){
segments++;
}
}
}
// lathe radius
if (key =='a'){
if (latheRadius>0){
latheRadius--;
}
}
else if (key == 's'){
latheRadius++;
}
// ellipse radius
if (key =='z'){
if (radius>10){
radius--;
}
}
else if (key == 'x'){
radius++;
}
// wireframe
if (key =='w'){
if (isWireFrame){
isWireFrame=false;
}
else {
isWireFrame=true;
}
}
// helix
if (key =='h'){
if (isHelix){
isHelix=false;
}
else {
isHelix=true;
}
}
}

View File

@@ -0,0 +1,67 @@
/**
* Vertices
* by Simon Greenwold.
*
* Draw a cylinder centered on the y-axis, going down
* from y=0 to y=height. The radius at the top can be
* different from the radius at the bottom, and the
* number of sides drawn is variable.
*/
void setup() {
size(640, 360, P3D);
}
void draw() {
background(0);
lights();
translate(width / 2, height / 2);
rotateY(map(mouseX, 0, width, 0, PI));
rotateZ(map(mouseY, 0, height, 0, -PI));
noStroke();
fill(255, 255, 255);
translate(0, -40, 0);
drawCylinder(10, 180, 200, 16); // Draw a mix between a cylinder and a cone
//drawCylinder(70, 70, 120, 64); // Draw a cylinder
//drawCylinder(0, 180, 200, 4); // Draw a pyramid
}
void drawCylinder(float topRadius, float bottomRadius, float tall, int sides) {
float angle = 0;
float angleIncrement = TWO_PI / sides;
beginShape(QUAD_STRIP);
for (int i = 0; i < sides + 1; ++i) {
vertex(topRadius*cos(angle), 0, topRadius*sin(angle));
vertex(bottomRadius*cos(angle), tall, bottomRadius*sin(angle));
angle += angleIncrement;
}
endShape();
// If it is not a cone, draw the circular top cap
if (topRadius != 0) {
angle = 0;
beginShape(TRIANGLE_FAN);
// Center point
vertex(0, 0, 0);
for (int i = 0; i < sides + 1; i++) {
vertex(topRadius * cos(angle), 0, topRadius * sin(angle));
angle += angleIncrement;
}
endShape();
}
// If it is not a cone, draw the circular bottom cap
if (bottomRadius != 0) {
angle = 0;
beginShape(TRIANGLE_FAN);
// Center point
vertex(0, tall, 0);
for (int i = 0; i < sides + 1; i++) {
vertex(bottomRadius * cos(angle), tall, bottomRadius * sin(angle));
angle += angleIncrement;
}
endShape();
}
}

View File

@@ -0,0 +1,18 @@
PShape rocket;
public void setup() {
size(640, 360, P3D);
rocket = loadShape("rocket.obj");
}
public void draw() {
background(0);
lights();
translate(width/2, height/2 + 100, -200);
rotateX(PI);
rotateY(frameCount * 0.01f);
shape(rocket);
}

View File

@@ -0,0 +1,8 @@
newmtl Default
illum 2
Ka 0.698039 0.698039 0.698039
Kd 0.698039 0.698039 0.698039
Ks 0.710000 0.710000 0.710000
Ns 76.109253
map_Kd rocket.png

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,35 @@
PShape world;
float resizeFactor;
float offsetX, offsetY;
void setup() {
size(1400, 700, P2D);
smooth(4);
world = loadShape("World_map_with_nations.svg");
PVector top = world.getTop();
world.translate(-top.x, -top.y);
float r = world.getWidth() / world.getHeight();
float w = width;
float h = width / r;
if (height < h) {
h = height;
w = r * h;
offsetX = (width - w) / 2;
offsetY = 0;
} else {
offsetX = 0;
offsetY = (height - h) / 2;
}
resizeFactor = w / world.getWidth();
world.scale(resizeFactor);
}
public void draw() {
shape(world);
}

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 586 KiB

View File

@@ -0,0 +1,35 @@
import processing.dxf.*;
PShape rocket;
boolean record;
public void setup() {
size(640, 360, P3D);
rocket = loadShape("rocket.obj");
}
public void draw() {
if (record) {
beginRaw(DXF, "rocket.dxf");
}
background(0);
lights();
translate(width/2, height/2 + 100, -200);
rotateX(PI);
rotateY(frameCount * 0.01f);
shape(rocket);
if (record) {
endRaw();
record = false;
}
}
void keyPressed() {
if (key == 'r') record = true;
}

View File

@@ -0,0 +1,8 @@
newmtl Default
illum 2
Ka 0.698039 0.698039 0.698039
Kd 0.698039 0.698039 0.698039
Ks 0.710000 0.710000 0.710000
Ns 76.109253
map_Kd rocket.png

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,46 @@
// Robot 1: Draw from "Getting Started with Processing"
// by Reas & Fry. O'Reilly / Make 2010
import processing.pdf.*;
size(720, 480, P2D);
smooth();
strokeWeight(2);
background(204);
ellipseMode(RADIUS);
beginRaw(PDF, "robot.pdf");
// Neck
stroke(102); // Set stroke to gray
line(266, 257, 266, 162); // Left
line(276, 257, 276, 162); // Middle
line(286, 257, 286, 162); // Right
// Antennae
line(276, 155, 246, 112); // Small
line(276, 155, 306, 56); // Tall
line(276, 155, 342, 170); // Medium
// Body
noStroke(); // Disable stroke
fill(102); // Set fill to gray
ellipse(264, 377, 33, 33); // Antigravity orb
fill(0); // Set fill to black
rect(219, 257, 90, 120); // Main body
fill(102); // Set fill to gray
rect(219, 274, 90, 6); // Gray stripe
// Head
fill(0); // Set fill to black
ellipse(276, 155, 45, 45); // Head
fill(255); // Set fill to white
ellipse(288, 150, 14, 14); // Large eye
fill(0); // Set fill to black
ellipse(288, 150, 3, 3); // Pupil
fill(153); // Set fill to light gray
ellipse(263, 148, 5, 5); // Small eye 1
ellipse(296, 130, 4, 4); // Small eye 2
ellipse(305, 162, 3, 3); // Small eye 3
endRaw();

View File

@@ -0,0 +1,80 @@
// Blending, by Andres Colubri
// Images can be blended using one of the 10 blending modes
// available in P3D.
// Images by Kevin Bjorke.
PImage pic1, pic2;
int selMode = REPLACE;
String name = "replace";
int picAlpha = 255;
void setup() {
size(800, 480, P3D);
PFont font = createFont(PFont.list()[0], 20);
textFont(font, 20);
pic1 = loadImage("bjorke1.jpg");
pic2 = loadImage("bjorke2.jpg");
}
void draw() {
background(0);
tint(255, 255);
image(pic1, 0, 0, pic1.width, pic1.height);
blendMode(selMode);
tint(255, picAlpha);
image(pic2, 0, 0, pic2.width, pic2.height);
blendMode(REPLACE);
fill(200, 50, 50);
rect(0, height - 50, map(picAlpha, 0, 255, 0, width), 50);
fill(255);
text("Selected blend mode: " + name + ". Click to move to next", 10, pic1.height + 30);
text("Drag this bar to change alpha of image", 10, height - 18);
}
void mousePressed() {
if (height - 50 < mouseY) return;
if (selMode == REPLACE) {
selMode = BLEND;
name = "blend";
} else if (selMode == BLEND) {
selMode = ADD;
name = "add";
} else if (selMode == ADD) {
selMode = SUBTRACT;
name = "subtract";
} else if (selMode == SUBTRACT) {
selMode = LIGHTEST;
name = "lightest";
} else if (selMode == LIGHTEST) {
selMode = DARKEST;
name = "darkest";
} else if (selMode == DARKEST) {
selMode = DIFFERENCE;
name = "difference";
} else if (selMode == DIFFERENCE) {
selMode = EXCLUSION;
name = "exclusion";
} else if (selMode == EXCLUSION) {
selMode = MULTIPLY;
name = "multiply";
} else if (selMode == MULTIPLY) {
selMode = SCREEN;
name = "screen";
} else if (selMode == SCREEN) {
selMode = REPLACE;
name = "replace";
}
}
void mouseDragged() {
if (height - 50 < mouseY) {
picAlpha = int(map(mouseX, 0, width, 0, 255));
}
}

View File

@@ -0,0 +1,43 @@
/**
* Explode
* by Daniel Shiffman.
*
* Mouse horizontal location controls breaking apart of image and
* Maps pixels from a 2D image into 3D space. Pixel brightness controls
* translation along z axis.
*/
PImage img; // The source image
int cellsize = 2; // Dimensions of each cell in the grid
int columns, rows; // Number of columns and rows in our system
void setup() {
size(640, 360, P3D);
img = loadImage("eames.jpg"); // Load the image
columns = img.width / cellsize; // Calculate # of columns
rows = img.height / cellsize; // Calculate # of rows
}
void draw() {
background(0);
// Begin loop for columns
for ( int i = 0; i < columns; i++) {
// Begin loop for rows
for ( int j = 0; j < rows; j++) {
int x = i*cellsize + cellsize/2; // x position
int y = j*cellsize + cellsize/2; // y position
int loc = x + y*img.width; // Pixel array location
color c = img.pixels[loc]; // Grab the color
// Calculate a z position as a function of mouseX and pixel brightness
float z = (mouseX / float(width)) * brightness(img.pixels[loc]) - 20.0;
// Translate to the location, set fill and stroke, and draw the rect
pushMatrix();
translate(x + 200, y + 100, z);
fill(c, 204);
noStroke();
rectMode(CENTER);
rect(0, 0, cellsize, cellsize);
popMatrix();
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

View File

@@ -0,0 +1,46 @@
/**
* Extrusion.
*
* Converts a flat image into spatial data points and rotates the points
* around the center.
*/
PImage extrude;
int[][] values;
float angle = 0;
void setup() {
size(640, 360, P3D);
// Load the image into a new array
extrude = loadImage("ystone08.jpg");
extrude.loadPixels();
values = new int[extrude.width][extrude.height];
for (int y = 0; y < extrude.height; y++) {
for (int x = 0; x < extrude.width; x++) {
color pixel = extrude.get(x, y);
values[x][y] = int(brightness(pixel));
}
}
}
void draw() {
background(0);
// Update the angle
angle += 0.005;
// Rotate around the center axis
translate(width/2, 0, -128);
rotateY(angle);
translate(-extrude.width/2, 100, -128);
// Display the image mass
for (int y = 0; y < extrude.height; y++) {
for (int x = 0; x < extrude.width; x++) {
stroke(values[x][y]);
point(x, y, -values[x][y]);
}
}
}

View File

@@ -0,0 +1,49 @@
/**
* Extrusion.
*
* Converts a flat image into spatial data points and rotates the points
* around the center.
*/
PImage a;
boolean onetime = true;
int[][] aPixels;
int[][] values;
float angle;
void setup() {
size(1024, 768, P3D);
aPixels = new int[width][height];
values = new int[width][height];
noFill();
// Load the image into a new array
// Extract the values and store in an array
a = loadImage("ystone08.jpg");
a.loadPixels();
for (int i = 0; i < a.height; i++) {
for (int j = 0; j < a.width; j++) {
aPixels[j][i] = a.pixels[i*a.width + j];
values[j][i] = int(blue(aPixels[j][i]));
}
}
}
void draw() {
background(255);
translate(width/2, height/2, 0);
scale(2.0);
// Update and constrain the angle
angle += 0.005;
rotateY(angle);
// Display the image mass
for (int i = 0; i < a.height; i += 2) {
for (int j = 0; j < a.width; j += 2) {
stroke(values[j][i], 153);
line(j-a.width/2, i-a.height/2, -values[j][i], j-a.width/2, i-a.height/2, -values[j][i]-10);
}
}
}

View File

@@ -0,0 +1,89 @@
/**
* Zoom.
*
* Move the cursor over the image to alter its position. Click and press
* the mouse to zoom and set the density of the matrix by typing numbers 1-5.
* This program displays a series of lines with their heights corresponding to
* a color value read from an image.
*/
PImage img;
//boolean onetime = true;
int[][] imgPixels;
float sval = 1.0;
float nmx, nmy;
int res = 5;
void setup()
{
size(640, 360, P3D);
noFill();
stroke(255);
img = loadImage("ystone08.jpg");
imgPixels = new int[img.width][img.height];
for (int i = 0; i < img.height; i++) {
for (int j = 0; j < img.width; j++) {
imgPixels[j][i] = img.get(j, i);
}
}
}
void draw()
{
background(0);
nmx = nmx + (mouseX-nmx)/20;
nmy += (mouseY-nmy)/20;
if(mousePressed) {
sval += 0.005;
}
else {
sval -= 0.01;
}
sval = constrain(sval, 1.0, 2.5);
translate(width/2 + nmx * sval-100, height/2 + nmy*sval - 200, -50);
scale(sval);
rotateZ(PI/9 - sval + 1.0);
rotateX(PI/sval/8 - 0.125);
rotateY(sval/8 - 0.125);
translate(-width/2, -height/2, 0);
for (int i = 0; i < img.height; i += res) {
for (int j = 0; j < img.width; j += res) {
float rr = red(imgPixels[j][i]);
float gg = green(imgPixels[j][i]);
float bb = blue(imgPixels[j][i]);
float tt = rr+gg+bb;
stroke(rr, gg, gg);
line(i, j, tt/10-20, i, j, tt/10 );
}
}
}
void keyPressed() {
if(key == '1') {
res = 1;
}
else if (key == '2') {
res = 2;
}
else if (key == '3') {
res = 3;
}
else if (key == '4') {
res = 4;
}
else if (key == '5') {
res = 5;
}
}

View File

@@ -0,0 +1,22 @@
/**
* Bezier.
*
* The first two parameters for the bezier() function specify the
* first point in the curve and the last two parameters specify
* the last point. The middle parameters set the control points
* that define the shape of the curve.
*/
void setup() {
size(640, 360, P3D);
stroke(255);
noFill();
}
void draw() {
background(0);
for (int i = 0; i < 200; i += 20) {
bezier(mouseX-(i/2.0), 40+i, 410, 20, 440, 300, 240-(i/16.0), 300+(i/8.0));
}
}

View File

@@ -0,0 +1,40 @@
// Robot 1: Draw from "Getting Started with Processing"
// by Reas & Fry. O'Reilly / Make 2010
size(720, 480, P2D);
smooth();
strokeWeight(2);
background(204);
ellipseMode(RADIUS);
// Neck
stroke(102); // Set stroke to gray
line(266, 257, 266, 162); // Left
line(276, 257, 276, 162); // Middle
line(286, 257, 286, 162); // Right
// Antennae
line(276, 155, 246, 112); // Small
line(276, 155, 306, 56); // Tall
line(276, 155, 342, 170); // Medium
// Body
noStroke(); // Disable stroke
fill(102); // Set fill to gray
ellipse(264, 377, 33, 33); // Antigravity orb
fill(0); // Set fill to black
rect(219, 257, 90, 120); // Main body
fill(102); // Set fill to gray
rect(219, 274, 90, 6); // Gray stripe
// Head
fill(0); // Set fill to black
ellipse(276, 155, 45, 45); // Head
fill(255); // Set fill to white
ellipse(288, 150, 14, 14); // Large eye
fill(0); // Set fill to black
ellipse(288, 150, 3, 3); // Pupil
fill(153); // Set fill to light gray
ellipse(263, 148, 5, 5); // Small eye 1
ellipse(296, 130, 4, 4); // Small eye 2
ellipse(305, 162, 3, 3); // Small eye 3

View File

@@ -0,0 +1,32 @@
/**
* Shape Primitives.
*
* The basic shape primitive functions are triangle(),
* rect(), quad(), ellipse(), and arc(). Squares are made
* with rect() and circles are made with ellipse(). Each
* of these functions requires a number of parameters to
* determine the shape's position and size.
*/
size(640, 360, P3D);
background(0);
noStroke();
fill(204);
triangle(18, 18, 18, 360, 81, 360);
fill(102);
rect(81, 81, 63, 63);
fill(204);
quad(189, 18, 216, 18, 216, 360, 144, 360);
fill(255);
ellipse(252, 144, 72, 72);
fill(204);
triangle(288, 18, 351, 360, 288, 360);
fill(255);
arc(479, 300, 280, 280, PI, TWO_PI);

View File

@@ -0,0 +1,45 @@
/**
* TRIANGLE_STRIP Mode
* by Ira Greenberg.
*
* Generate a closed ring using the vertex() function and
* beginShape(TRIANGLE_STRIP) mode. The outerRad and innerRad
* variables control ring's radii respectively.
*/
int x;
int y;
float outerRad;
float innerRad;
void setup() {
size(640, 360, P2D);
smooth(4);
background(204);
x = width/2;
y = height/2;
outerRad = min(width, height) * 0.4;
innerRad = outerRad * 0.6;
}
void draw() {
background(204);
int pts = int(map(mouseX, 0, width, 6, 60));
float rot = 180.0/pts;
float angle = 0;
beginShape(TRIANGLE_STRIP);
for (int i = 0; i <= pts; i++) {
float px = x + cos(radians(angle)) * outerRad;
float py = y + sin(radians(angle)) * outerRad;
angle += rot;
vertex(px, py);
px = x + cos(radians(angle)) * innerRad;
py = y + sin(radians(angle)) * innerRad;
vertex(px, py);
angle += rot;
}
endShape();
}

View File

@@ -0,0 +1,39 @@
// CameraLight, by Andres Colubri
// Simple example showing a lit rotating cube. The projection
// is set to orthographic if the mouse is pressed.
float spin = 0.0;
void setup() {
size(400, 400, P3D);
noStroke();
}
void draw() {
background(51);
lights();
if (mousePressed) {
ortho(-width/2, width/2, -height/2, height/2);
} else {
float fov = PI/3.0;
float cameraZ = (height/2.0) / tan(fov/2.0);
perspective(fov, float(width)/float(height),
cameraZ/2.0, cameraZ*2.0);
}
spin += 0.01;
pushMatrix();
translate(width/2, height/2, 0);
rotateX(PI/9);
rotateY(PI/5 + spin);
box(100);
popMatrix();
}

View File

@@ -0,0 +1,28 @@
/**
* Directional.
*
* Move the mouse the change the direction of the light.
* Directional light comes from one direction and is stronger
* when hitting a surface squarely and weaker if it hits at a
* a gentle angle. After hitting a surface, a directional lights
* scatters in all directions.
*/
void setup() {
size(640, 360, P3D);
noStroke();
fill(204);
}
void draw() {
noStroke();
background(0);
float dirY = (mouseY / float(height) - 0.5) * 2;
float dirX = (mouseX / float(width) - 0.5) * 2;
directionalLight(204, 204, 204, -dirX, -dirY, -1);
translate(width/2 - 100, height/2, 0);
sphere(80);
translate(200, 0, 0);
sphere(80);
}

View File

@@ -0,0 +1,29 @@
/**
* Lights 1.
*
* Uses the default lights to show a simple box. The lights() function
* is used to turn on the default lighting.
*/
float spin = 0.0;
void setup()
{
size(640, 360, P3D);
noStroke();
}
void draw()
{
background(51);
lights();
spin += 0.01;
pushMatrix();
translate(width/2, height/2, 0);
rotateX(PI/9);
rotateY(PI/5 + spin);
box(150);
popMatrix();
}

View File

@@ -0,0 +1,36 @@
/**
* Lights 2
* by Simon Greenwold.
*
* Display a box with three different kinds of lights.
*/
void setup()
{
size(640, 360, P3D);
noStroke();
}
void draw()
{
background(0);
translate(width / 2, height / 2);
// Orange point light on the right
pointLight(150, 100, 0, // Color
200, -150, 0); // Position
// Blue directional light from the left
directionalLight(0, 102, 255, // Color
1, 0, 0); // The x-, y-, z-axis direction
// Yellow spotlight from the front
spotLight(255, 255, 109, // Color
0, 40, 200, // Position
0, -0.5, -0.5, // Direction
PI / 2, 2); // Angle, concentration
rotateY(map(mouseX, 0, width, 0, PI));
rotateX(map(mouseY, 0, height, 0, PI));
box(150);
}

View File

@@ -0,0 +1,45 @@
/**
* LightsGL.
* Modified from an example by Simon Greenwold.
*
* Display a box with three different kinds of lights.
*/
void setup()
{
size(1024, 768, P3D);
noStroke();
}
void draw()
{
defineLights();
background(0);
for (int x = 0; x <= width; x += 100) {
for (int y = 0; y <= height; y += 100) {
pushMatrix();
translate(x, y);
rotateY(map(mouseX, 0, width, 0, PI));
rotateX(map(mouseY, 0, height, 0, PI));
box(90);
popMatrix();
}
}
}
void defineLights() {
// Orange point light on the right
pointLight(150, 100, 0, // Color
200, -150, 0); // Position
// Blue directional light from the left
directionalLight(0, 102, 255, // Color
1, 0, 0); // The x-, y-, z-axis direction
// Yellow spotlight from the front
spotLight(255, 255, 109, // Color
0, 40, 200, // Position
0, -0.5, -0.5, // Direction
PI / 2, 2); // Angle, concentration
}

View File

@@ -0,0 +1,25 @@
/**
* Reflection
* by Simon Greenwold.
*
* Vary the specular reflection component of a material
* with the horizontal position of the mouse.
*/
void setup() {
size(640, 360, P3D);
noStroke();
colorMode(RGB, 1);
fill(0.4);
}
void draw() {
background(0);
translate(width / 2, height / 2);
// Set the specular color of lights that follow
lightSpecular(1, 1, 1);
directionalLight(0.8, 0.8, 0.8, 0, 0, -1);
float s = mouseX / float(width);
specular(s, s, s);
sphere(120);
}

View File

@@ -0,0 +1,35 @@
/**
* Spot.
*
* Move the mouse the change the position and concentation
* of a blue spot light.
*/
int concentration = 600; // Try values 1 -> 10000
void setup()
{
//size(200, 200, P3D);
size(640, 360, P3D);
noStroke();
fill(204);
sphereDetail(60);
}
void draw()
{
background(0);
// Light the bottom of the sphere
directionalLight(51, 102, 126, 0, -1, 0);
// Orange light on the upper-right of the sphere
spotLight(204, 153, 0, 360, 160, 600, 0, 0, -1, PI/2, 600);
// Moving spotlight that follows the mouse
spotLight(102, 153, 204, 360, mouseY, 600, 0, 0, -1, PI/2, 600);
translate(width/2, height/2, 0);
sphere(120);
}

View File

@@ -0,0 +1,65 @@
/**
* Cubic Grid
* by Ira Greenberg.
*
* 3D translucent colored grid uses nested pushMatrix()
* and popMatrix() functions.
*/
float boxSize = 20;
float margin = boxSize*2;
float depth = 400;
color boxFill;
int fcount, lastm;
float frate;
int fint = 3;
void setup() {
size(640, 360, P3D);
frameRate(60);
noSmooth();
noStroke();
}
void draw() {
background(255);
hint(DISABLE_DEPTH_TEST);
// Center and spin grid
pushMatrix();
translate(width/2, height/2, -depth);
rotateY(frameCount * 0.01);
rotateX(frameCount * 0.01);
// Build grid using multiple translations
for (float i =- depth/2+margin; i <= depth/2-margin; i += boxSize){
for (float j =- height+margin; j <= height-margin; j += boxSize){
for (float k =- width+margin; k <= width-margin; k += boxSize){
// Base fill color on counter values, abs function
// ensures values stay within legal range
boxFill = color(abs(i), abs(j), abs(k), 50);
pushMatrix();
translate(k, j, i);
fill(boxFill);
box(boxSize, boxSize, boxSize);
popMatrix();
}
}
}
popMatrix();
hint(ENABLE_DEPTH_TEST);
fcount += 1;
int m = millis();
if (m - lastm > 1000 * fint) {
frate = float(fcount) / fint;
fcount = 0;
lastm = m;
println("fps: " + frate);
}
fill(0);
text("fps: " + frate, 10, 20);
}

View File

@@ -0,0 +1,62 @@
float boxSize = 20;
float margin = boxSize*2;
float depth = 400;
color boxFill;
PShape grid;
int fcount, lastm;
float frate;
int fint = 3;
void setup() {
size(640, 360, P3D);
frameRate(60);
noSmooth();
noStroke();
grid = createShape(PShape.GROUP);
// Build grid using multiple translations
for (float i =- depth/2+margin; i <= depth/2-margin; i += boxSize){
for (float j =- height+margin; j <= height-margin; j += boxSize){
for (float k =- width+margin; k <= width-margin; k += boxSize){
// Base fill color on counter values, abs function
// ensures values stay within legal range
boxFill = color(abs(i), abs(j), abs(k), 50);
PShape cube = createShape(BOX, boxSize, boxSize, boxSize);
cube.fill(boxFill);
cube.translate(k, j, i);
grid.addChild(cube);
}
}
}
}
void draw() {
background(255);
hint(DISABLE_DEPTH_TEST);
// Center and spin grid
pushMatrix();
translate(width/2, height/2, -depth);
rotateY(frameCount * 0.01);
rotateX(frameCount * 0.01);
shape(grid);
popMatrix();
hint(ENABLE_DEPTH_TEST);
fcount += 1;
int m = millis();
if (m - lastm > 1000 * fint) {
frate = float(fcount) / fint;
fcount = 0;
lastm = m;
println("fps: " + frate);
}
fill(0);
text("fps: " + frate, 10, 20);
}

View File

@@ -0,0 +1,118 @@
PImage sprite;
int npartTotal = 10000;
int npartPerFrame = 25;
float speed = 1.0;
float gravity = 0.05;
float partSize = 20;
int partLifetime;
PVector positions[];
PVector velocities[];
int lifetimes[];
int fcount, lastm;
float frate;
int fint = 3;
void setup() {
size(640, 480, P3D);
frameRate(120);
sprite = loadImage("sprite.png");
partLifetime = npartTotal / npartPerFrame;
initPositions();
initVelocities();
initLifetimes();
// Writing to the depth buffer is disabled to avoid rendering
// artifacts due to the fact that the particles are semi-transparent
// but not z-sorted.
hint(DISABLE_DEPTH_MASK);
// Testing some hints
//hint(DISABLE_TRANSFORM_CACHE);
//hint(ENABLE_ACCURATE_2D);
}
void draw () {
background(0);
for (int n = 0; n < npartTotal; n++) {
lifetimes[n]++;
if (lifetimes[n] == partLifetime) {
lifetimes[n] = 0;
}
if (0 <= lifetimes[n]) {
float opacity = 1.0 - float(lifetimes[n]) / partLifetime;
if (lifetimes[n] == 0) {
// Re-spawn dead particle
positions[n].x = mouseX;
positions[n].y = mouseY;
float angle = random(0, TWO_PI);
float s = random(0.5 * speed, 0.5 * speed);
velocities[n].x = s * cos(angle);
velocities[n].y = s * sin(angle);
} else {
positions[n].x += velocities[n].x;
positions[n].y += velocities[n].y;
velocities[n].y += gravity;
}
drawParticle(positions[n], opacity);
}
}
fcount += 1;
int m = millis();
if (m - lastm > 1000 * fint) {
frate = float(fcount) / fint;
fcount = 0;
lastm = m;
println("fps: " + frate);
}
}
void drawParticle(PVector center, float opacity) {
beginShape(QUAD);
noStroke();
tint(255, opacity * 255);
texture(sprite);
normal(0, 0, 1);
vertex(center.x - partSize/2, center.y - partSize/2, 0, 0);
vertex(center.x + partSize/2, center.y - partSize/2, sprite.width, 0);
vertex(center.x + partSize/2, center.y + partSize/2, sprite.width, sprite.height);
vertex(center.x - partSize/2, center.y + partSize/2, 0, sprite.height);
endShape();
}
void initPositions() {
positions = new PVector[npartTotal];
for (int n = 0; n < positions.length; n++) {
positions[n] = new PVector();
}
}
void initVelocities() {
velocities = new PVector[npartTotal];
for (int n = 0; n < velocities.length; n++) {
velocities[n] = new PVector();
}
}
void initLifetimes() {
// Initializing particles with negative lifetimes so they are added
// progressively into the screen during the first frames of the sketch
lifetimes = new int[npartTotal];
int t = -1;
for (int n = 0; n < lifetimes.length; n++) {
if (n % npartPerFrame == 0) {
t++;
}
lifetimes[n] = -t;
}
}

View File

@@ -0,0 +1,110 @@
PShape particles;
PImage sprite;
int npartTotal = 10000;
int npartPerFrame = 25;
float speed = 1.0;
float gravity = 0.05;
float partSize = 20;
int partLifetime;
PVector velocities[];
int lifetimes[];
int fcount, lastm;
float frate;
int fint = 3;
void setup() {
size(640, 480, P3D);
frameRate(120);
particles = createShape(PShape.GROUP);
sprite = loadImage("sprite.png");
for (int n = 0; n < npartTotal; n++) {
PShape part = createShape(QUAD);
part.noStroke();
part.texture(sprite);
part.normal(0, 0, 1);
part.vertex(-partSize/2, -partSize/2, 0, 0);
part.vertex(+partSize/2, -partSize/2, sprite.width, 0);
part.vertex(+partSize/2, +partSize/2, sprite.width, sprite.height);
part.vertex(-partSize/2, +partSize/2, 0, sprite.height);
part.end();
particles.addChild(part);
}
partLifetime = npartTotal / npartPerFrame;
initVelocities();
initLifetimes();
// Writing to the depth buffer is disabled to avoid rendering
// artifacts due to the fact that the particles are semi-transparent
// but not z-sorted.
hint(DISABLE_DEPTH_MASK);
}
void draw () {
background(0);
for (int n = 0; n < particles.getChildCount(); n++) {
PShape part = particles.getChild(n);
lifetimes[n]++;
if (lifetimes[n] == partLifetime) {
lifetimes[n] = 0;
}
if (0 <= lifetimes[n]) {
float opacity = 1.0 - float(lifetimes[n]) / partLifetime;
part.tint(255, opacity * 255);
if (lifetimes[n] == 0) {
// Re-spawn dead particle
part.resetMatrix();
part.translate(mouseX, mouseY);
float angle = random(0, TWO_PI);
float s = random(0.5 * speed, 0.5 * speed);
velocities[n].x = s * cos(angle);
velocities[n].y = s * sin(angle);
} else {
part.translate(velocities[n].x, velocities[n].y);
velocities[n].y += gravity;
}
} else {
part.tint(0, 0);
}
}
shape(particles);
fcount += 1;
int m = millis();
if (m - lastm > 1000 * fint) {
frate = float(fcount) / fint;
fcount = 0;
lastm = m;
println("fps: " + frate);
}
}
void initVelocities() {
velocities = new PVector[npartTotal];
for (int n = 0; n < velocities.length; n++) {
velocities[n] = new PVector();
}
}
void initLifetimes() {
// Initializing particles with negative lifetimes so they are added
// progressively into the screen during the first frames of the sketch
lifetimes = new int[npartTotal];
int t = -1;
for (int n = 0; n < lifetimes.length; n++) {
if (n % npartPerFrame == 0) {
t++;
}
lifetimes[n] = -t;
}
}

View File

@@ -0,0 +1,91 @@
/**
* Esfera
* by David Pena.
*
* Distribucion aleatoria uniforme sobre la superficie de una esfera.
*/
int cuantos = 16000;
pelo[] lista ;
float[] z = new float[cuantos];
float[] phi = new float[cuantos];
float[] largos = new float[cuantos];
float radio = 200;
float rx = 0;
float ry =0;
void setup() {
size(1024, 768, P3D);
noSmooth();
frameRate(120);
radio = height/3.5;
lista = new pelo[cuantos];
for (int i=0; i<cuantos; i++){
lista[i] = new pelo();
}
noiseDetail(3);
}
void draw() {
background(0);
translate(width/2,height/2);
float rxp = ((mouseX-(width/2))*0.005);
float ryp = ((mouseY-(height/2))*0.005);
rx = (rx*0.9)+(rxp*0.1);
ry = (ry*0.9)+(ryp*0.1);
rotateY(rx);
rotateX(ry);
fill(0);
noStroke();
sphere(radio);
for (int i=0;i<cuantos;i++){
lista[i].dibujar();
}
if (frameCount % 10 == 0) {
println(frameRate);
}
}
class pelo
{
float z = random(-radio,radio);
float phi = random(TWO_PI);
float largo = random(1.15,1.2);
float theta = asin(z/radio);
void dibujar(){
float off = (noise(millis() * 0.0005,sin(phi))-0.5) * 0.3;
float offb = (noise(millis() * 0.0007,sin(z) * 0.01)-0.5) * 0.3;
float thetaff = theta+off;
float phff = phi+offb;
float x = radio * cos(theta) * cos(phi);
float y = radio * cos(theta) * sin(phi);
float z = radio * sin(theta);
float msx= screenX(x,y,z);
float msy= screenY(x,y,z);
float xo = radio * cos(thetaff) * cos(phff);
float yo = radio * cos(thetaff) * sin(phff);
float zo = radio * sin(thetaff);
float xb = xo * largo;
float yb = yo * largo;
float zb = zo * largo;
strokeWeight(1);
beginShape(LINES);
stroke(0);
vertex(x,y,z);
stroke(200,150);
vertex(xb,yb,zb);
endShape();
}
}

View File

@@ -0,0 +1,25 @@
import processing.opengl.*;
public void setup() {
size(800, 600, OPENGL);
}
public void draw() {
background(255);
stroke(0, 10);
for (int i = 0; i < 50000; i++) {
float x0 = random(width);
float y0 = random(height);
float z0 = random(-100, 100);
float x1 = random(width);
float y1 = random(height);
float z1 = random(-100, 100);
// purely 2D lines will trigger the GLU
// tessellator to add accurate line caps,
// but performance will be substantially
// lower.
line(x0, y0, z0, x1, y1, z1);
}
if (frameCount % 10 == 0) println(frameRate);
}

View File

@@ -0,0 +1,18 @@
import processing.opengl.*;
public void setup() {
size(800, 600, OPENGL);
noStroke();
fill(0, 1);
}
public void draw() {
background(255);
for (int i = 0; i < 50000; i++) {
float x = random(width);
float y = random(height);
rect(x, y, 30, 30);
}
if (frameCount % 10 == 0) println(frameRate);
}

View File

@@ -0,0 +1,65 @@
PImage sprite;
int npartTotal = 50000;
float partSize = 20;
PVector positions[];
int fcount, lastm;
float frate;
int fint = 3;
void setup() {
size(800, 600, P3D);
frameRate(60);
sprite = loadImage("sprite.png");
initPositions();
// Writing to the depth buffer is disabled to avoid rendering
// artifacts due to the fact that the particles are semi-transparent
// but not z-sorted.
hint(DISABLE_DEPTH_MASK);
}
void draw () {
background(0);
translate(width/2, height/2);
rotateY(frameCount * 0.01);
for (int n = 0; n < npartTotal; n++) {
drawParticle(positions[n]);
}
fcount += 1;
int m = millis();
if (m - lastm > 1000 * fint) {
frate = float(fcount) / fint;
fcount = 0;
lastm = m;
println("fps: " + frate);
}
}
void drawParticle(PVector center) {
beginShape(QUAD);
noStroke();
tint(255);
texture(sprite);
normal(0, 0, 1);
vertex(center.x - partSize/2, center.y - partSize/2, center.z, 0, 0);
vertex(center.x + partSize/2, center.y - partSize/2, center.z, sprite.width, 0);
vertex(center.x + partSize/2, center.y + partSize/2, center.z, sprite.width, sprite.height);
vertex(center.x - partSize/2, center.y + partSize/2, center.z, 0, sprite.height);
endShape();
}
void initPositions() {
positions = new PVector[npartTotal];
for (int n = 0; n < positions.length; n++) {
positions[n] = new PVector(random(-500, +500), random(-500, +500), random(-500, +500));
}
}

View File

@@ -0,0 +1,59 @@
PShape particles;
PImage sprite;
int npartTotal = 50000;
float partSize = 20;
int fcount, lastm;
float frate;
int fint = 3;
void setup() {
size(800, 600, P3D);
frameRate(60);
particles = createShape(PShape.GROUP);
sprite = loadImage("sprite.png");
for (int n = 0; n < npartTotal; n++) {
float cx = random(-500, +500);
float cy = random(-500, +500);
float cz = random(-500, +500);
PShape part = createShape(QUAD);
part.noStroke();
part.tint(255);
part.texture(sprite);
part.normal(0, 0, 1);
part.vertex(cx - partSize/2, cy - partSize/2, cz, 0, 0);
part.vertex(cx + partSize/2, cy - partSize/2, cz, sprite.width, 0);
part.vertex(cx + partSize/2, cy + partSize/2, cz, sprite.width, sprite.height);
part.vertex(cx - partSize/2, cy + partSize/2, cz, 0, sprite.height);
part.end();
particles.addChild(part);
}
// Writing to the depth buffer is disabled to avoid rendering
// artifacts due to the fact that the particles are semi-transparent
// but not z-sorted.
hint(DISABLE_DEPTH_MASK);
}
void draw () {
background(0);
translate(width/2, height/2);
rotateY(frameCount * 0.01);
shape(particles);
fcount += 1;
int m = millis();
if (m - lastm > 1000 * fint) {
frate = float(fcount) / fint;
fcount = 0;
lastm = m;
println("fps: " + frate);
}
}

View File

@@ -0,0 +1,16 @@
import processing.opengl.*;
public void setup() {
size(800, 600, OPENGL);
fill(0);
}
public void draw() {
background(255);
for (int i = 0; i < 10000; i++) {
float x = random(width);
float y = random(height);
text("HELLO", x, y);
}
if (frameCount % 10 == 0) println(frameRate);
}

View File

@@ -0,0 +1,30 @@
void setup() {
size(255, 255, P3D);
// for (int x = 0; x < width; x++) {
// for (int y = 0; y < height; y++) {
// int c = color(x, y, 0);
// set(x, y, c);
// }
// }
loadPixels();
for (int i = 0; i < pixels.length; i++) {
int x = i % width;
int y = i / height;
int c = color(x, y, 0);
set(x, y, c);
}
updatePixels();
}
void draw() {
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int c = get(x, y);
set(x, y, c);
}
}
println(frameRate);
}

View File

@@ -0,0 +1,16 @@
void setup() {
size(400, 400, P3D);
background(0);
}
void draw() {
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int c = color(random(255), random(255), random(255));
set(x, y, c);
}
}
println(frameRate);
}

View File

@@ -0,0 +1,15 @@
void setup() {
size(400, 400, P3D);
background(0);
loadPixels();
}
void draw() {
for (int i = 0; i < pixels.length; i++) {
pixels[i] = color(random(255), random(255), random(255));
}
updatePixels();
println(frameRate);
}

View File

@@ -0,0 +1,21 @@
size(100, 100, P2D);
beginShape(POLYGON);
fill(0, 255, 0, 255);
stroke(0, 0, 255, 255);
strokeWeight(5);
strokeJoin(ROUND);
beginContour();
vertex(10, 10);
vertex(10, 90);
vertex(90, 90);
vertex(90, 10);
endContour();
beginContour();
vertex(40, 40);
vertex(70, 40);
vertex(70, 70);
vertex(40, 70);
endContour();
endShape(CLOSE);

View File

@@ -0,0 +1,22 @@
size(100, 100, P2D);
PShape obj = createShape(POLYGON);
obj.fill(0, 255, 0, 255);
obj.stroke(0, 0, 255, 255);
obj.strokeWeight(5);
obj.strokeJoin(ROUND);
obj.beginContour();
obj.vertex(10, 10);
obj.vertex(10, 90);
obj.vertex(90, 90);
obj.vertex(90, 10);
obj.endContour();
obj.beginContour();
obj.vertex(40, 40);
obj.vertex(70, 40);
obj.vertex(70, 70);
obj.vertex(40, 70);
obj.endContour();
obj.end(CLOSE);
shape(obj);

View File

@@ -0,0 +1,42 @@
PShape circle;
boolean filled = true;
boolean stroked = true;
public void setup() {
size(400, 400, P2D);
fill(255, 0, 0);
stroke(0, 0, 255);
strokeWeight(3);
circle = createShape(ELLIPSE, 0, 0, 100, 100);
}
public void draw () {
background(0);
if (5000 < millis()) {
if (filled) circle.fill(map(mouseX, 0, width, 255, 0), 0, 0);
if (stroked) circle.stroke(0, 0, map(mouseY, 0, height, 255, 0));
}
translate(mouseX, mouseY);
shape(circle);
}
void keyPressed() {
if (key == 's') {
if (stroked) {
circle.noStroke();
stroked = false;
} else {
stroked = true;
}
} else if (key == 'f') {
if (filled) {
circle.noFill();
filled = false;
} else {
filled = true;
}
}
}

View File

@@ -0,0 +1,15 @@
size(100, 100, P2D);
smooth();
PShape obj = createShape();
obj.noFill();
obj.stroke(0);
obj.strokeWeight(1);
obj.vertex(15, 40); // V1 (see p.76)
obj.bezierVertex(5, 0, 80, 0, 50, 55); // C1, C2, V2
obj.vertex(30, 45); // V3
obj.vertex(25, 75); // V4
obj.bezierVertex(50, 70, 75, 90, 80, 70); // C3, C4, V5
obj.end();
shape(obj);

View File

@@ -0,0 +1,71 @@
// Bad-print shader. Adapted from rom Fluxus Shader Library:
// http://www.pawfal.org/index.php?page=FluxusHardwareShaderLibrary
import controlP5.*;
ControlP5 controlP5;
PShader badPrint;
boolean enabled = true;
float scaleR = 1.0, scaleG = 1.0, scaleB = 1.0;
float offsetR = 0.2, offsetG = 0.2, offsetB = 0.2;
float registerR = 0.2, registerG = 0.2, registerB = 0.2;
float sizeR = 0.1, sizeG = 0.2, sizeB = 0.1;
public void setup() {
size(800, 800, P3D);
noStroke();
fill(204);
badPrint = (PShader)loadShader("BadPrintVert.glsl", "BadPrintFrag.glsl", PShader.LIT);
shader(badPrint, PShader.LIT);
sphereDetail(60);
controlP5 = new ControlP5(this);
controlP5.addToggle("enabled").linebreak();
controlP5.addSlider("scaleR", 0, 1);
controlP5.addSlider("scaleG", 0, 1);
controlP5.addSlider("scaleB", 0, 1).linebreak();
controlP5.addSlider("offsetR", 0, 1);
controlP5.addSlider("offsetG", 0, 1);
controlP5.addSlider("offsetB", 0, 1).linebreak();
controlP5.addSlider("registerR", 0, 1);
controlP5.addSlider("registerG", 0, 1);
controlP5.addSlider("registerB", 0, 1).linebreak();
controlP5.addSlider("sizeR", 0, 1);
controlP5.addSlider("sizeG", 0, 1);
controlP5.addSlider("sizeB", 0, 1).linebreak();
}
public void draw() {
background(0);
if (enabled) {
shader(badPrint, PShader.LIT);
badPrint.set("Scale", scaleR, scaleG, scaleB);
badPrint.set("Offset", offsetR, offsetG, offsetB);
badPrint.set("Register", registerR, registerG, registerB);
badPrint.set("Size", sizeR, sizeG, sizeB);
} else {
resetShader(PShader.LIT);
}
noStroke();
pointLight(204, 204, 204, 1000, 1000, 1000);
pushMatrix();
translate(width/2, height/2);
rotateX(frameCount * 0.01);
rotateY(frameCount * 0.01);
sphere(200);
popMatrix();
hint(DISABLE_DEPTH_TEST);
noLights();
controlP5.draw();
hint(ENABLE_DEPTH_TEST);
}

View File

@@ -0,0 +1,36 @@
// Copyright (C) 2007 Dave Griffiths
// Licence: GPLv2 (see COPYING)
// Fluxus Shader Library
// ---------------------
// BadPrint NPR Shader
// This shader tries to emulate the effect
// of a bad printing process. Can be controlled
// with different settings for RGB
uniform vec3 Scale;
uniform vec3 Offset;
uniform vec3 Register;
uniform vec3 Size;
varying vec3 N;
varying vec3 P;
varying vec4 S;
varying vec3 L;
void main() {
vec3 l = normalize(L);
vec3 n = normalize(N);
vec2 p = S.xy;
vec2 sr = p * Size.r + Register.r;
vec2 sg = p * Size.g + Register.g;
vec2 sb = p * Size.b + Register.b;
float diffuse = dot(l,n);
float r = (sin(sr.x) + cos(sr.y)) * Scale.r + Offset.r + diffuse;
float g = (sin(sg.x) + cos(sg.y)) * Scale.g + Offset.g + diffuse;
float b = (sin(sb.x) + cos(sb.y)) * Scale.b + Offset.b + diffuse;
gl_FragColor = vec4(step(0.5, r), step(0.5, g), step(0.5, b), 1);
}

View File

@@ -0,0 +1,30 @@
// Copyright (C) 2007 Dave Griffiths
// Licence: GPLv2 (see COPYING)
// Fluxus Shader Library
// ---------------------
// BadPrint NPR Shader
// This shader tries to emulate the effect
// of a bad printing process. Can be controlled
// with different settings for RGB
uniform mat4 projectionMatrix;
uniform mat4 projmodelviewMatrix;
uniform mat3 normalMatrix;
uniform vec4 lightPosition[8];
attribute vec4 inVertex;
attribute vec3 inNormal;
varying vec3 N;
varying vec3 P;
varying vec4 S;
varying vec3 L;
void main() {
N = normalize(normalMatrix * inNormal);
P = inVertex.xyz;
gl_Position = projmodelviewMatrix * inVertex;
L = vec3(lightPosition[0] - gl_Position);
S = projectionMatrix * gl_Position;
}

View File

@@ -0,0 +1,32 @@
PShader blur;
boolean applyFilter = true;
void setup() {
size(400, 400, P3D);
blur = (PShader)loadShader("blur.glsl");
noStroke();
}
void draw() {
background(0);
lights();
translate(width/2, height/2);
pushMatrix();
rotateX(frameCount * 0.01);
rotateY(frameCount * 0.01);
box(100);
popMatrix();
if (applyFilter) filter(blur);
// The sphere doesn't have blur applied on it
// because it is drawn after filter() is called.
rotateY(frameCount * 0.02);
translate(150, 0);
sphere(40);
}
void mousePressed() {
applyFilter = !applyFilter;
}

View File

@@ -0,0 +1,43 @@
uniform sampler2D textureSampler;
uniform vec2 texcoordOffset;
varying vec4 vertColor;
varying vec4 vertTexcoord;
#define KERNEL_SIZE 9
// Gaussian kernel
// 1 2 1
// 2 4 2
// 1 2 1
float kernel[KERNEL_SIZE];
vec2 offset[KERNEL_SIZE];
void main(void) {
int i = 0;
vec4 sum = vec4(0.0);
offset[0] = vec2(-texcoordOffset.s, -texcoordOffset.t);
offset[1] = vec2(0.0, -texcoordOffset.t);
offset[2] = vec2(texcoordOffset.s, -texcoordOffset.t);
offset[3] = vec2(-texcoordOffset.s, 0.0);
offset[4] = vec2(0.0, 0.0);
offset[5] = vec2(texcoordOffset.s, 0.0);
offset[6] = vec2(-texcoordOffset.s, texcoordOffset.t);
offset[7] = vec2(0.0, texcoordOffset.t);
offset[8] = vec2(texcoordOffset.s, texcoordOffset.t);
kernel[0] = 1.0/16.0; kernel[1] = 2.0/16.0; kernel[2] = 1.0/16.0;
kernel[3] = 2.0/16.0; kernel[4] = 4.0/16.0; kernel[5] = 2.0/16.0;
kernel[6] = 1.0/16.0; kernel[7] = 2.0/16.0; kernel[8] = 1.0/16.0;
for(i = 0; i < KERNEL_SIZE; i++) {
vec4 tmp = texture2D(textureSampler, vertTexcoord.st + offset[i]);
sum += tmp * kernel[i];
}
gl_FragColor = vec4(sum.rgb, 1.0) * vertColor;
}

View File

@@ -0,0 +1,32 @@
// This example shows how to change the default fragment shader used
// in P2D to render textures, by a custom one that applies a simple
// edge detection filter.
//
// Press the mouse to switch between the custom and the default shader.
PImage img;
PShader edges;
boolean customShader;
void setup() {
size(400, 400, P2D);
img = loadImage("berlin-1.jpg");
edges = (PShader)loadShader("edges.glsl", PShader.TEXTURED);
shader(edges, PShader.TEXTURED);
customShader = true;
}
public void draw() {
image(img, 0, 0, width, height);
}
public void mousePressed() {
if (customShader) {
resetShader(PShader.TEXTURED);
customShader = false;
} else {
shader(edges, PShader.TEXTURED);
customShader = true;
}
}

View File

@@ -0,0 +1,38 @@
// Edge detection shader
uniform sampler2D textureSampler;
// The inverse of the texture dimensions along X and Y
uniform vec2 texcoordOffset;
varying vec4 vertColor;
varying vec4 vertTexcoord;
void main() {
vec4 sum = vec4(0);
float kernel[9];
kernel[0] = -1.0; kernel[1] = -1.0; kernel[2] = -1.0;
kernel[3] = -1.0; kernel[4] = +8.0; kernel[5] = -1.0;
kernel[6] = -1.0; kernel[7] = -1.0; kernel[8] = -1.0;
vec2 offset[9];
offset[0] = vec2(-texcoordOffset.s, -texcoordOffset.t);
offset[1] = vec2( 0.0, -texcoordOffset.t);
offset[2] = vec2(+texcoordOffset.s, -texcoordOffset.t);
offset[3] = vec2(-texcoordOffset.s, 0.0);
offset[4] = vec2( 0.0, 0.0);
offset[5] = vec2(+texcoordOffset.s, 0.0);
offset[6] = vec2(-texcoordOffset.s, +texcoordOffset.t);
offset[7] = vec2( 0.0, +texcoordOffset.t);
offset[8] = vec2(+texcoordOffset.s, +texcoordOffset.t);
for (int i = 0; i < 9; i++) {
vec4 tmp = texture2D(textureSampler, vertTexcoord.st + offset[i]);
sum += tmp * kernel[i];
}
gl_FragColor = vec4(sum.rgb, 1.0);
}

View File

@@ -0,0 +1,11 @@
PShader edges;
void setup() {
size(400, 400, P2D);
edges = (PShader)loadShader("edges.glsl");
}
void draw() {
rect(mouseX, mouseY, 50, 50);
filter(edges);
}

View File

@@ -0,0 +1,43 @@
uniform sampler2D textureSampler;
uniform vec2 texcoordOffset;
varying vec4 vertColor;
varying vec4 vertTexcoord;
#define KERNEL_SIZE 9
// Edge detection kernel
// -1 -1 -1
// -1 +8 -1
// -1 -1 -1
float kernel[KERNEL_SIZE];
vec2 offset[KERNEL_SIZE];
void main(void) {
int i = 0;
vec4 sum = vec4(0.0);
offset[0] = vec2(-texcoordOffset.s, -texcoordOffset.t);
offset[1] = vec2(0.0, -texcoordOffset.t);
offset[2] = vec2(texcoordOffset.s, -texcoordOffset.t);
offset[3] = vec2(-texcoordOffset.s, 0.0);
offset[4] = vec2(0.0, 0.0);
offset[5] = vec2(texcoordOffset.s, 0.0);
offset[6] = vec2(-texcoordOffset.s, texcoordOffset.t);
offset[7] = vec2(0.0, texcoordOffset.t);
offset[8] = vec2(texcoordOffset.s, texcoordOffset.t);
kernel[0] = -1.0; kernel[1] = -1.0; kernel[2] = -1.0;
kernel[3] = -1.0; kernel[4] = 8.0; kernel[5] = -1.0;
kernel[6] = -1.0; kernel[7] = -1.0; kernel[8] = -1.0;
for(i = 0; i < KERNEL_SIZE; i++) {
vec4 tmp = texture2D(textureSampler, vertTexcoord.st + offset[i]);
sum += tmp * kernel[i];
}
gl_FragColor = vec4(sum.rgb, 1.0) * vertColor;
}

View File

@@ -0,0 +1,92 @@
// This example uses a FxAA post-processing filter for fast
// fullscreen antialiasing:
// http://www.kotaku.com.au/2011/12/what-is-fxaa/
//
// Press any key to enable/disable the shader.
PGraphics canvas;
boolean drawing = false;
PShader fxaa;
boolean usingShader;
String message;
float msgLen;
void setup() {
size(1280, 800, P2D);
noSmooth();
canvas = createGraphics(width, height, P2D);
canvas.noSmooth();
fxaa = (PShader)loadShader("fxaa.glsl", PShader.TEXTURED);
shader(fxaa, PShader.TEXTURED);
usingShader = true;
canvas.beginDraw();
canvas.background(255);
canvas.stroke(0);
canvas.strokeWeight(15);
canvas.strokeCap(ROUND);
canvas.endDraw();
PFont font = createFont("Arial", 18);
textFont(font);
updateMessage();
drawing = false;
}
public void draw() {
if (drawing) {
canvas.beginDraw();
if (1 < dist(mouseX, mouseY, pmouseX, pmouseY)) {
canvas.line(pmouseX, pmouseY, mouseX, mouseY);
}
canvas.endDraw();
}
image(canvas, 0, 0);
drawMessage();
}
public void mousePressed() {
if (!drawing && width - msgLen < mouseX && height - 23 < mouseY) {
if (usingShader) {
resetShader(PShader.TEXTURED);
usingShader = false;
} else {
shader(fxaa, PShader.TEXTURED);
usingShader = true;
}
updateMessage();
} else {
drawing = true;
}
}
void mouseReleased() {
drawing = false;
}
void updateMessage() {
if (usingShader) {
message = "Anti-aliasing enabled";
} else {
message = "Anti-aliasing disabled";
}
msgLen = textWidth(message);
}
void drawMessage() {
if (usingShader) {
// We need the default texture shader to
// render text.
resetShader(PShader.TEXTURED);
}
fill(0);
text(message, width - msgLen, height - 5);
if (usingShader) {
shader(fxaa, PShader.TEXTURED);
}
}

View File

@@ -0,0 +1,69 @@
// FXAA shader, GLSL code adapted from:
// http://horde3d.org/wiki/index.php5?title=Shading_Technique_-_FXAA
// Whitepaper describing the technique:
// http://developer.download.nvidia.com/assets/gamedev/files/sdk/11/FXAA_WhitePaper.pdf
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif
uniform sampler2D textureSampler;
// The inverse of the texture dimensions along X and Y
uniform vec2 texcoordOffset;
varying vec4 vertColor;
varying vec4 vertTexcoord;
void main() {
// The parameters are hardcoded for now, but could be
// made into uniforms to control fromt he program.
float FXAA_SPAN_MAX = 8.0;
float FXAA_REDUCE_MUL = 1.0/8.0;
float FXAA_REDUCE_MIN = (1.0/128.0);
vec3 rgbNW = texture2D(textureSampler, vertTexcoord.xy + (vec2(-1.0, -1.0) * texcoordOffset)).xyz;
vec3 rgbNE = texture2D(textureSampler, vertTexcoord.xy + (vec2(+1.0, -1.0) * texcoordOffset)).xyz;
vec3 rgbSW = texture2D(textureSampler, vertTexcoord.xy + (vec2(-1.0, +1.0) * texcoordOffset)).xyz;
vec3 rgbSE = texture2D(textureSampler, vertTexcoord.xy + (vec2(+1.0, +1.0) * texcoordOffset)).xyz;
vec3 rgbM = texture2D(textureSampler, vertTexcoord.xy).xyz;
vec3 luma = vec3(0.299, 0.587, 0.114);
float lumaNW = dot(rgbNW, luma);
float lumaNE = dot(rgbNE, luma);
float lumaSW = dot(rgbSW, luma);
float lumaSE = dot(rgbSE, luma);
float lumaM = dot( rgbM, luma);
float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
vec2 dir;
dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN);
float rcpDirMin = 1.0/(min(abs(dir.x), abs(dir.y)) + dirReduce);
dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), dir * rcpDirMin)) * texcoordOffset;
vec3 rgbA = (1.0/2.0) * (
texture2D(textureSampler, vertTexcoord.xy + dir * (1.0/3.0 - 0.5)).xyz +
texture2D(textureSampler, vertTexcoord.xy + dir * (2.0/3.0 - 0.5)).xyz);
vec3 rgbB = rgbA * (1.0/2.0) + (1.0/4.0) * (
texture2D(textureSampler, vertTexcoord.xy + dir * (0.0/3.0 - 0.5)).xyz +
texture2D(textureSampler, vertTexcoord.xy + dir * (3.0/3.0 - 0.5)).xyz);
float lumaB = dot(rgbB, luma);
if((lumaB < lumaMin) || (lumaB > lumaMax)){
gl_FragColor.xyz=rgbA;
} else {
gl_FragColor.xyz=rgbB;
}
gl_FragColor.a = 1.0;
gl_FragColor *= vertColor;
}

View File

@@ -0,0 +1,37 @@
// Fish-eye shader, useful for dome projection
PGraphics canvas;
PShader fisheye;
PImage img;
void setup() {
size(400, 400, P3D);
canvas = createGraphics(400, 400, P3D);
fisheye = (PShader)loadShader("FishEye.glsl", PShader.TEXTURED);
fisheye.set("aperture", 180.0);
shader(fisheye, PShader.TEXTURED);
}
void draw() {
canvas.beginDraw();
canvas.background(0);
canvas.stroke(255, 0, 0);
for (int i = 0; i < width; i += 10) {
canvas.line(i, 0, i, height);
}
for (int i = 0; i < height; i += 10) {
canvas.line(0, i, width, i);
}
canvas.lights();
canvas.noStroke();
canvas.translate(mouseX, mouseY, 100);
canvas.rotateX(frameCount * 0.01f);
canvas.rotateY(frameCount * 0.01f);
canvas.box(50);
canvas.endDraw();
// The rendering of this image will be done through the fisheye shader, since
// it was set as the PShader.TEXTURED shader of the main surface.
image(canvas, 0, 0, width, height);
}

View File

@@ -0,0 +1,46 @@
// Inspired by the "Angular Fisheye à la Bourke" sketch from
// Jonathan Cremieux, as shown in the OpenProcessing website:
// http://openprocessing.org/visuals/?visualID=12140
// Using the inverse transform of the angular fisheye as
// explained in Paul Bourke's website:
// http://paulbourke.net/miscellaneous/domefisheye/fisheye/
uniform sampler2D textureSampler;
varying vec4 vertColor;
varying vec4 vertTexcoord;
uniform float aperture;
const float PI = 3.1415926535;
void main(void) {
float apertureHalf = 0.5 * aperture * (PI / 180.0);
// This factor ajusts the coordinates in the case that
// the aperture angle is less than 180 degrees, in which
// case the area displayed is not the entire half-sphere.
float maxFactor = sin(apertureHalf);
vec2 pos = 2.0 * vertTexcoord.st - 1.0;
float l = length(pos);
if (l > 1.0) {
gl_FragColor = vec4(0, 0, 0, 1);
} else {
float x = maxFactor * pos.x;
float y = maxFactor * pos.y;
float n = length(vec2(x, y));
float z = sqrt(1.0 - n * n);
float r = atan(n, z) / PI;
float phi = atan(y, x);
float u = r * cos(phi) + 0.5;
float v = r * sin(phi) + 0.5;
gl_FragColor = texture2D(textureSampler, vec2(u, v)) * vertColor;
}
}

View File

@@ -0,0 +1,70 @@
// Fish-eye shader on the main surface, glossy specular reflection shader
// on the offscreen canvas.
PGraphics canvas;
PShader fisheye;
PShader glossy;
PImage img;
PShape ball;
boolean usingFishEye;
void setup() {
size(800, 800, P3D);
canvas = createGraphics(800, 800, P3D);
fisheye = (PShader)loadShader("FishEye.glsl", PShader.TEXTURED);
fisheye.set("aperture", 180.0);
shader(fisheye, PShader.TEXTURED);
usingFishEye = true;
glossy = (PShader)loadShader("GlossyVert.glsl", "GlossyFrag.glsl", PShader.LIT);
glossy.set("AmbientColour", 0, 0, 0);
glossy.set("DiffuseColour", 0.9, 0.2, 0.2);
glossy.set("SpecularColour", 1.0, 1.0, 1.0);
glossy.set("AmbientIntensity", 1.0);
glossy.set("DiffuseIntensity", 1.0);
glossy.set("SpecularIntensity", 0.7);
glossy.set("Roughness", 0.7);
glossy.set("Sharpness", 0.0);
canvas.shader(glossy, PShader.LIT);
ball = createShape(SPHERE, 50);
//ball.fill(200, 50, 50);
ball.noStroke();
}
void draw() {
canvas.beginDraw();
canvas.noStroke();
canvas.background(0);
canvas.pushMatrix();
canvas.rotateY(frameCount * 0.01);
canvas.pointLight(204, 204, 204, 1000, 1000, 1000);
canvas.popMatrix();
for (float x = 0; x < canvas.width + 100; x += 100) {
for (float y = 0; y < canvas.height + 100; y += 100) {
for (float z = 0; z < 400; z += 100) {
canvas.pushMatrix();
canvas.translate(x, y, -z);
canvas.shape(ball);
canvas.popMatrix();
}
}
}
canvas.endDraw();
image(canvas, 0, 0, width, height);
println(frameRate);
}
public void mousePressed() {
if (usingFishEye) {
resetShader(PShader.TEXTURED);
usingFishEye = false;
} else {
shader(fisheye, PShader.TEXTURED);
usingFishEye = true;
}
}

View File

@@ -0,0 +1,46 @@
// Inspired by the "Angular Fisheye à la Bourke" sketch from
// Jonathan Cremieux, as shown in the OpenProcessing website:
// http://openprocessing.org/visuals/?visualID=12140
// Using the inverse transform of the angular fisheye as
// explained in Paul Bourke's website:
// http://paulbourke.net/miscellaneous/domefisheye/fisheye/
uniform sampler2D textureSampler;
varying vec4 vertColor;
varying vec4 vertTexcoord;
uniform float aperture;
const float PI = 3.1415926535;
void main(void) {
float apertureHalf = 0.5 * aperture * (PI / 180.0);
// This factor ajusts the coordinates in the case that
// the aperture angle is less than 180 degrees, in which
// case the area displayed is not the entire half-sphere.
float maxFactor = sin(apertureHalf);
vec2 pos = 2.0 * vertTexcoord.st - 1.0;
float l = length(pos);
if (l > 1.0) {
gl_FragColor = vec4(0, 0, 0, 1);
} else {
float x = maxFactor * pos.x;
float y = maxFactor * pos.y;
float n = length(vec2(x, y));
float z = sqrt(1.0 - n * n);
float r = atan(n, z) / PI;
float phi = atan(y, x);
float u = r * cos(phi) + 0.5;
float v = r * sin(phi) + 0.5;
gl_FragColor = texture2D(textureSampler, vec2(u, v)) * vertColor;
}
}

Some files were not shown because too many files have changed in this diff Show More