processing
Sunday, December 25, 2005
going 3d
void setup()
{
size(900, 200, P3D);
noStroke();
}
void draw()
{
background(255);
lights();
//ortho(0, width, 0, height, -10, 10); // Default ortho settings
ortho(-width, mouseX, -height/2.0, mouseY/2.0, -10, 10);
translate(0, 0, -100);
rotateX(PI/4);
rotateZ(PI/3);
pushMatrix();
for(int i=0; i < width; i+=20) {
for(int j=0; j < height; j+=20) {
box(10, 10, (j+i) / 4.0);
translate(20, 0, 0);
}
translate(-200, 20, 0);
}
popMatrix();
}
// OrthoVSPerspective
// Click to see the difference between orthographic projection
// and perspective projection as applied to a simple box.
void setup()
{
size(200, 200, P3D);
noStroke();
fill(204);
}
void draw()
{
background(0);
lights();
if(mousePressed) {
float fov = 60.0;
float cameraZ = (height/2.0) / tan(PI * fov / 360.0);
perspective(fov, float(width)/float(height),
cameraZ/10.0, cameraZ*10.0);
} else {
ortho(-width/2, width/2, -height/2, height/2, -10, 10);
}
translate(100, 100);
rotateX(-PI/6);
rotateY(PI/3);
box(85);
}
// 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(200, 200, 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);
}
// 3-D FORM
// 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(200, 200, P3D);
background(0);
lights();
noStroke();
pushMatrix();
translate(47, height/2, 0);
rotateY(0.75);
box(50);
popMatrix();
pushMatrix();
translate(200, height/2, 0);
sphere(100);
popMatrix();
// 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(200, 200, P3D);
noStroke();
colorMode(RGB, 1);
}
void draw()
{
background(0.5, 0.5, 0.45);
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(50);
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();
}
// Vertices 3D
// Scaling an object back in space makes it appear smaller.
float spin = PI/6; // Rotation angle
int circle_points = 6; // Resolution of arcs
int depth = 10;
float size = 120.0;
float thick = 50.0;
void setup()
{
size(200, 200, P3D);
framerate(30);
background(0);
}
void draw()
{
noStroke();
fill(0, 10);
rect(0, 0, width, height);
translate(width/2, height/2);
for(int i=0; i < depth; i++) {
stroke(255 - (i * 22.5));
rotate(spin);
drawSpokes(-i*20);
pushMatrix();
rotate(PI);
drawSpokes(-i*20);
popMatrix();
}
spin += 0.002;
if(spin > PI) {
spin = 0;
}
}
void drawSpokes(float z_space)
{
beginShape(LINES);
for (int i=0; i < circle_points; i++) {
float angle = PI/2.0 * i/circle_points;
vertex(size*cos(angle), size*sin(angle), z_space);
vertex((size-thick)*cos(angle), (size-thick)*sin(angle), z_space);
}
endShape();
}
3-D IMAGE
// Extrusion
// Converts a flat image into spatial data points.
// Created 18 August 2002
PImage a;
boolean onetime = true;
int[][] aPixels;
int[][] values;
float angle;
void setup()
{
size(200, 200, P3D);
framerate(24);
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");
for(int i=0; i < height; i++) {
for(int j=0; j< width; j++) {
aPixels[j][i] = a.pixels[i*width + j];
values[j][i] = int(blue(aPixels[j][i]));
}
}
framerate(30);
}
void draw()
{
background(255);
// Update and constrain the angle
angle += 0.005;
if(angle > TWO_PI) { angle = 0; }
// Rotate around the center axis
translate(width/2, 0, 128);
rotateY(angle);
translate(-width/2, 0, 128);
// Display the image mass
for(int i=0; i < height; i+=2) {
for(int j=0; j < width; j+=2) {
stroke(values[j][i]);
point(j, i, -values[j][i]);
}
}
}
// 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 a;
boolean onetime = true;
int[][] aPixels;
float sval = 1.0;
float nmx, nmy;
int res = 5;
void setup()
{
size(200, 200, P3D);
noFill();
stroke(255);
aPixels = new int[width][height];
a = loadImage("ystone08.jpg");
for(int i=0; i < height; i++) {
for(int j=0; j aPixels[j][i] = a.pixels[i*width+j];
}
}
}
void draw()
{
background(51);
if (abs(mouseX - nmx) > 1.0) {
nmx = nmx + (mouseX-nmx)/20;
}
if (abs(mouseY - nmy) > 1.0) {
nmy += (mouseY-nmy)/20;
}
if(mousePressed) {
sval += 0.005;
} else {
sval -= 0.01;
}
if(sval > 2.5) { sval = 2.5; }
if(sval < 1.0) { sval = 1.0; }
translate(width/2+nmx*sval-100, height/2+nmy*sval-100, -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);
float rr, gg, bb, tt;
for(int i=0; i < height; i+=res) {
for(int j=0; j rr = red(aPixels[j][i]);
gg = green(aPixels[j][i]);
bb = blue(aPixels[j][i]);
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;
}
}
3-D LIGHTS
// 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(200, 200, P3D);
noStroke();
fill(204);
}
void draw()
{
noStroke();
background(0);
float dirY = (mouseY/float(height) - 0.5) * 2.0;
float dirX = (mouseX/float(width) - 0.5) * 2.0;
directionalLight(204, 204, 204, -dirX, -dirY, -1);
translate(20, height/2, 0);
sphere(60);
translate(120, 0, 0);
sphere(60);
}
// Lights
// Uses the default lights to show a simple box
float spin = 0.0;
void setup()
{
size(200, 200, P3D);
framerate(30);
noStroke();
}
void draw()
{
background(51);
lights();
spin += 0.01;
pushMatrix();
translate(width/2, height/2, 0);
rotateX(PI/9);
rotateY(PI/5 + spin);
box(90);
popMatrix();
}
// Spot
// Move the mouse the change the position and concentation
// of a blue spot light.
int concentration = 600; // Try 1 -> 10000
void setup()
{
size(200, 200, P3D);
noStroke();
fill(204);
sphereDetail(60);
}
void draw()
{
background(0);
directionalLight(51, 102, 126, 0, -1, 0);
spotLight(204, 153, 0, 120, 80, 400, 0, 0, -1, PI/2, 600);
spotLight(102, 153, 204, 120, mouseY, 400, 0, 0, -1, PI/2, (mouseX * 90) + 180);
translate(160, 100, 0);
sphere(90);
}
3-D TYPOGRAPHY
// Kinetic Type
// Using the push() pop() defines the curves of the lines of type.
Line ln;
Line lns[];
PFont f;
String words[] = {
"sometimes it's like", "the lines of text", "are so happy", "that they want to dance",
"or leave the page or jump", "can you blame them?", "living on the page like that",
"waiting to be read..."
};
void setup()
{
size(200, 200, P3D);
framerate(30);
// Array of line objects
lns = new Line[8];
// Load the font from the sketch's data directory
f = loadFont("Univers66.vlw.gz");
textFont(f, 1f);
// White type, black background
fill(255);
// Creating the line objects
for(int i = 0; i < 8; i++)
{
// For every line in the array, create a Line object to animate
// i * 70 is the spacing
ln = new Line(words[i], 0, i * 70, f);
lns[i] = ln;
}
}
void draw()
{
background(0);
translate((float)(width / 2.0) - 350, (height / 2.0) - 240, -450);
rotateY(0.3);
// Now animate every line object & draw it...
for(int i = 0; i < 8; i++)
{
float f1 = sin((i + 1.0) * (millis() / 10000.0) * TWO_PI);
float f2 = sin((8.0 - i) * (millis() / 10000.0) * TWO_PI);
Line line = lns[i];
pushMatrix();
translate(0.0, line.yPosition, 0.0);
for(int j = 0; j < line.myLetters.length; j++)
{
if(j != 0) {
translate(textWidth(line.myLetters[j - 1].myChar)*75, 0.0, 0.0);
}
rotateY(f1 * 0.035 * f2);
pushMatrix();
scale(75.0, 75.0, 75.0);
text(line.myLetters[j].myChar, 0.0, 0.0);
popMatrix();
}
popMatrix();
}
}
class Letter
{
char myChar;
float x;
float y;
Letter(char c, float f, float f1)
{
myChar = c;
x = f;
y = f1;
}
}
class Word
{
String myName;
int x;
Word(String s)
{
myName = s;
}
}
class Line
{
String myString;
int xPosition;
int yPosition;
int highlightNum;
PFont f;
float speed;
float curlInX;
Letter myLetters[];
Line(String s, int i, int j, PFont bagelfont)
{
myString = s;
xPosition = i;
yPosition = j;
f = bagelfont;
myLetters = new Letter[s.length()];
float f1 = 0.0;
for(int k = 0; k < s.length(); k++)
{
char c = s.charAt(k);
f1 += textWidth(c);
Letter letter = new Letter(c, f1, 0.0);
myLetters[k] = letter;
}
curlInX = 0.1;
}
}
// Letter K
// Move the mouse across the screen to fold the "K"
color bgc, fgc, fgc2;
float p_x, p_y;
float p_fx, p_fy;
float p_v2, p_vx, p_vy;
float p_a2, p_ax, p_ay;
float p_mass, p_drag;
void setup()
{
size(200, 200, P3D);
noStroke();
colorMode(RGB, 255);
bgc = color(134, 144, 154);
fgc = color(235, 235, 30);
fgc2 = color(240, 130, 20);
init_particle(.6, .9, width/2, height/2);
}
void draw()
{
background(bgc);
pushMatrix();
iterate_particle(.15*(-p_x+mouseX), .15*(-p_y+(height-mouseY)));
translate(width/2, height/2, 0);
fill(fgc);
drawK();
pushMatrix();
translate(0, 0, 1);
translate(.75*(p_x-width/2), -.75*(p_y-height/2), 0);
translate(.75*(p_x-width/2), -.75*(p_y-height/2), 0);
rotateZ(atan2(-(p_y-height/2),(p_x-width/2)) + PI/2);
rotateX(PI);
rotateZ(-(atan2(-(p_y-height/2),(p_x-width/2)) + PI/2));
fill(fgc2);
drawK();
popMatrix();
translate(0, 0, 2);
translate(.75*(p_x-width/2), -.75*(p_y-height/2), 0);
rotateZ(atan2(-(p_y-height/2),(p_x-width/2)) + PI/2);
fill(bgc);
beginShape(QUADS);
vertex(-200, 0);
vertex(+200, 0);
vertex(+200, -200);
vertex(-200, -200);
endShape();
popMatrix();
}
void init_particle(float _mass, float _drag, float ox, float oy)
{
p_x = ox;
p_y = oy;
p_v2 = 0.0f;
p_vx = 0.0f;
p_vy = 0.0f;
p_a2 = 0.0f;
p_ax = 0.0f;
p_ay = 0.0f;
p_mass = _mass;
p_drag = _drag;
}
void iterate_particle(float fkx, float fky)
{
// iterate for a single force acting on the particle
p_fx = fkx;
p_fy = fky;
p_a2 = p_fx*p_fx + p_fy*p_fy;
if (p_a2 < 0.0000001) return;
p_ax = p_fx/p_mass;
p_ay = p_fy/p_mass;
p_vx += p_ax;
p_vy += p_ay;
p_v2 = p_vx*p_vx + p_vy*p_vy;
if (p_v2 < 0.0000001) return;
p_vx *= (1.0 - p_drag);
p_vy *= (1.0 - p_drag);
p_x += p_vx;
p_y += p_vy;
}
void drawK()
{
scale(1);
translate(-63, +71);
beginShape(QUADS);
vertex(0, 0, 0);
vertex(0, -142.7979, 0);
vertex(37.1992, -142.7979, 0);
vertex(37.1992, 0, 0);
vertex(37.1992, -87.9990, 0);
vertex(84.1987, -142.7979, 0);
vertex(130.3979, -142.7979, 0);
vertex(37.1992, -43.999, 0);
vertex(77.5986-.2, -86.5986-.3, 0);
vertex(136.998, 0, 0);
vertex(90.7988, 0, 0);
vertex(52.3994-.2, -59.999-.3, 0);
endShape();
translate(+63, -71);
}
// Typing
// Click in the window to give it focus.
// Type to add letters and press backspace to delete.
PFont f;
int leftmargin = 10;
int rightmargin = 20;
String buff = "";
boolean didntTypeYet = true;
void setup()
{
size(200, 200, P3D);
// Load the font. Fonts are located within the
// main Processing directory/folder and they
// must be placed within the data directory
// of your sketch for them to load
f = loadFont("Univers45.vlw.gz");
textFont(f, 25);
}
void draw()
{
background(176);
if(millis()%500<250){ // Only fill cursor half the time
noFill();
}else{
fill(255);
stroke(0);
}
float rPos;
// Store the cursor rectangle's position
rPos = textWidth(buff)+leftmargin;
rect(rPos+1, 19, 10, 21);
// Some instructions at first
if(didntTypeYet){
fill(0);
//text("Use the keyboard.", 22, 40);
}
fill(0);
pushMatrix();
translate(rPos,10+25);
char k;
for(int i=0;i k = buff.charAt(i);
translate(-textWidth(k),0);
rotateY(-textWidth(k)/70.0);
rotateX(textWidth(k)/70.0);
scale(1.1);
text(k,0,0);
}
popMatrix();
}
void keyPressed()
{
char k;
k = (char)key;
switch(k){
case 8:
if(buff.length()>0){
buff = buff.substring(1);
}
break;
case 13: // Avoid special keys
case 10:
case 65535:
case 127:
case 27:
break;
default:
if(textWidth(buff+k)+leftmargin < width-rightmargin){
didntTypeYet = false;
buff=k+buff;
}
break;
}
}
posted by Jaan Ehlvest, 7:56 AM