Skip to content

Commit e453946

Browse files
committed
Adding new round of examples
1 parent 3a1d45e commit e453946

File tree

430 files changed

+15630
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

430 files changed

+15630
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Move Eye.
3+
* by Simon Greenwold.
4+
*
5+
* The camera lifts up (controlled by mouseY) while looking at the same point.
6+
*
7+
* Created 27 May 2007
8+
*/
9+
10+
void setup() {
11+
size(400, 400, P3D);
12+
fill(204);
13+
}
14+
15+
void draw() {
16+
lights();
17+
background(0);
18+
// Change height of the camera with mouseY
19+
camera(30.0, mouseY, 220.0, // eyeX, eyeY, eyeZ
20+
0.0, 0.0, 0.0, // centerX, centerY, centerZ
21+
0.0, 1.0, 0.0); // upX, upY, upZ
22+
noStroke();
23+
box(90);
24+
stroke(255);
25+
line(-100, 0, 0, 100, 0, 0);
26+
line(0, -100, 0, 0, 100, 0);
27+
line(0, 0, -100, 0, 0, 100);
28+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Ortho.
3+
*
4+
* Move the mouse across the screen to change the parameters
5+
* for the orthographic projection.
6+
* The ortho() function sets an orthographic projection and
7+
* defines a parallel clipping volume. All objects with the
8+
* same dimension appear the same size, regardless of whether
9+
* they are near or far from the camera. The parameters to this
10+
* function specify the clipping volume where left and right
11+
* are the minimum and maximum x values, top and bottom are the
12+
* minimum and maximum y values, and near and far are the minimum
13+
* and maximum z values.
14+
*
15+
* Created 28 April 2005
16+
*/
17+
18+
void setup()
19+
{
20+
size(200, 200, P3D);
21+
noStroke();
22+
}
23+
24+
void draw()
25+
{
26+
background(255);
27+
lights();
28+
29+
//ortho(0, width, 0, height, -10, 10); // Default ortho settings
30+
ortho(-width, mouseX, -height/2.0, mouseY/2.0, -10, 10);
31+
32+
translate(0, 0, -100);
33+
rotateX(PI/4);
34+
rotateZ(PI/3);
35+
36+
pushMatrix();
37+
for(int i=0; i<width; i+=20) {
38+
for(int j=0; j<height; j+=20) {
39+
box(10, 10, (j+i) / 4.0);
40+
translate(20, 0, 0);
41+
}
42+
translate(-200, 20, 0);
43+
}
44+
popMatrix();
45+
46+
}
47+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Ortho vs Perspective.
3+
*
4+
* Click to see the difference between orthographic projection
5+
* and perspective projection as applied to a simple box.
6+
*
7+
* Created 28 April 2005
8+
*/
9+
10+
void setup()
11+
{
12+
size(200, 200, P3D);
13+
noStroke();
14+
fill(204);
15+
}
16+
17+
void draw()
18+
{
19+
background(0);
20+
lights();
21+
22+
if(mousePressed) {
23+
float fov = PI/3.0;
24+
float cameraZ = (height/2.0) / tan(PI * fov / 360.0);
25+
perspective(fov, float(width)/float(height),
26+
cameraZ/2.0, cameraZ*2.0);
27+
} else {
28+
ortho(-width/2, width/2, -height/2, height/2, -10, 10);
29+
}
30+
31+
translate(100, 100, 0);
32+
rotateX(-PI/6);
33+
rotateY(PI/3);
34+
box(85);
35+
}
36+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Perspective.
3+
*
4+
* Move the mouse left and right to change the field of view (fov).
5+
* Click to modify the aspect ratio. The perspective() function
6+
* sets a perspective projection applying foreshortening, making
7+
* distant objects appear smaller than closer ones. The parameters
8+
* define a viewing volume with the shape of truncated pyramid.
9+
* Objects near to the front of the volume appear their actual size,
10+
* while farther objects appear smaller. This projection simulates
11+
* the perspective of the world more accurately than orthographic projection.
12+
* The version of perspective without parameters sets the default
13+
* perspective and the version with four parameters allows the programmer
14+
* to set the area precisely.
15+
*
16+
* Created 28 April 2005
17+
*/
18+
19+
void setup()
20+
{
21+
size(200, 200, P3D);
22+
noStroke();
23+
}
24+
25+
void draw()
26+
{
27+
lights();
28+
background(204);
29+
float cameraY = height/2.0;
30+
float fov = mouseX/float(width) * PI/2;
31+
float cameraZ = cameraY / tan(fov / 2.0);
32+
float aspect = float(width)/float(height);
33+
if(mousePressed) {
34+
aspect = aspect / 2.0;
35+
}
36+
perspective(fov, aspect, cameraZ/10.0, cameraZ*10.0);
37+
38+
translate(width/2+30, height/2, 0);
39+
rotateX(-PI/6);
40+
rotateY(PI/3 + mouseY/float(height) * PI);
41+
box(45);
42+
translate(0, 0, -50);
43+
box(30);
44+
}
45+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Primitives 3D.
3+
*
4+
* Placing mathematically 3D objects in synthetic space.
5+
* The lights() method reveals their imagined dimension.
6+
* The box() and sphere() functions each have one parameter
7+
* which is used to specify their size. These shapes are
8+
* positioned using the translate() function.
9+
*
10+
* Created 16 January 2003
11+
*/
12+
13+
size(200, 200, P3D);
14+
background(0);
15+
lights();
16+
noStroke();
17+
18+
pushMatrix();
19+
translate(47, height/2, 0);
20+
rotateY(0.75);
21+
box(50);
22+
popMatrix();
23+
24+
pushMatrix();
25+
translate(200, height/2, 0);
26+
sphere(100);
27+
popMatrix();
28+
29+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* RGB Cube.
3+
*
4+
* The three primary colors of the additive color model are red, green, and blue.
5+
* This RGB color cube displays smooth transitions between these colors.
6+
*
7+
* Created 25 October 2002
8+
*/
9+
10+
float xmag, ymag = 0;
11+
float newXmag, newYmag = 0;
12+
13+
void setup()
14+
{
15+
size(200, 200, P3D);
16+
noStroke();
17+
colorMode(RGB, 1);
18+
}
19+
20+
void draw()
21+
{
22+
background(0.5, 0.5, 0.45);
23+
24+
pushMatrix();
25+
26+
translate(width/2, height/2, -30);
27+
28+
newXmag = mouseX/float(width) * TWO_PI;
29+
newYmag = mouseY/float(height) * TWO_PI;
30+
31+
float diff = xmag-newXmag;
32+
if (abs(diff) > 0.01) { xmag -= diff/4.0; }
33+
34+
diff = ymag-newYmag;
35+
if (abs(diff) > 0.01) { ymag -= diff/4.0; }
36+
37+
rotateX(-ymag);
38+
rotateY(-xmag);
39+
40+
scale(50);
41+
beginShape(QUADS);
42+
43+
fill(0, 1, 1); vertex(-1, 1, 1);
44+
fill(1, 1, 1); vertex( 1, 1, 1);
45+
fill(1, 0, 1); vertex( 1, -1, 1);
46+
fill(0, 0, 1); vertex(-1, -1, 1);
47+
48+
fill(1, 1, 1); vertex( 1, 1, 1);
49+
fill(1, 1, 0); vertex( 1, 1, -1);
50+
fill(1, 0, 0); vertex( 1, -1, -1);
51+
fill(1, 0, 1); vertex( 1, -1, 1);
52+
53+
fill(1, 1, 0); vertex( 1, 1, -1);
54+
fill(0, 1, 0); vertex(-1, 1, -1);
55+
fill(0, 0, 0); vertex(-1, -1, -1);
56+
fill(1, 0, 0); vertex( 1, -1, -1);
57+
58+
fill(0, 1, 0); vertex(-1, 1, -1);
59+
fill(0, 1, 1); vertex(-1, 1, 1);
60+
fill(0, 0, 1); vertex(-1, -1, 1);
61+
fill(0, 0, 0); vertex(-1, -1, -1);
62+
63+
fill(0, 1, 0); vertex(-1, 1, -1);
64+
fill(1, 1, 0); vertex( 1, 1, -1);
65+
fill(1, 1, 1); vertex( 1, 1, 1);
66+
fill(0, 1, 1); vertex(-1, 1, 1);
67+
68+
fill(0, 0, 0); vertex(-1, -1, -1);
69+
fill(1, 0, 0); vertex( 1, -1, -1);
70+
fill(1, 0, 1); vertex( 1, -1, 1);
71+
fill(0, 0, 1); vertex(-1, -1, 1);
72+
73+
endShape();
74+
75+
popMatrix();
76+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Vertices.
3+
* by Simon Greenwold.
4+
*
5+
* Draw a cylinder centered on the y-axis, going down from y=0 to y=height.
6+
* The radius at the top can be different from the radius at the bottom,
7+
* and the number of sides drawn is variable.
8+
*
9+
* Created 27 May 2007
10+
*/
11+
12+
void setup() {
13+
size(200, 200, P3D);
14+
}
15+
16+
void draw() {
17+
background(0);
18+
lights();
19+
translate(width / 2, height / 2);
20+
rotateY(map(mouseX, 0, width, 0, PI));
21+
rotateZ(map(mouseY, 0, height, 0, -PI));
22+
noStroke();
23+
fill(255, 255, 255);
24+
translate(0, -40, 0);
25+
drawCylinder(10, 180, 200, 16); // Draw a mix between a cylinder and a cone
26+
//drawCylinder(70, 70, 120, 64); // Draw a cylinder
27+
//drawCylinder(0, 180, 200, 4); // Draw a pyramid
28+
}
29+
30+
void drawCylinder(float topRadius, float bottomRadius, float tall, int sides) {
31+
float angle = 0;
32+
float angleIncrement = TWO_PI / sides;
33+
beginShape(QUAD_STRIP);
34+
for (int i = 0; i < sides + 1; ++i) {
35+
vertex(topRadius*cos(angle), 0, topRadius*sin(angle));
36+
vertex(bottomRadius*cos(angle), tall, bottomRadius*sin(angle));
37+
angle += angleIncrement;
38+
}
39+
endShape();
40+
41+
// If it is not a cone, draw the circular top cap
42+
if (topRadius != 0) {
43+
angle = 0;
44+
beginShape(TRIANGLE_FAN);
45+
46+
// Center point
47+
vertex(0, 0, 0);
48+
for (int i = 0; i < sides + 1; i++) {
49+
vertex(topRadius * cos(angle), 0, topRadius * sin(angle));
50+
angle += angleIncrement;
51+
}
52+
endShape();
53+
}
54+
55+
// If it is not a cone, draw the circular bottom cap
56+
if (bottomRadius != 0) {
57+
angle = 0;
58+
beginShape(TRIANGLE_FAN);
59+
60+
// Center point
61+
vertex(0, tall, 0);
62+
for (int i = 0; i < sides + 1; i++) {
63+
vertex(bottomRadius * cos(angle), tall, bottomRadius * sin(angle));
64+
angle += angleIncrement;
65+
}
66+
endShape();
67+
}
68+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Explode
3+
* by <a href="http://www.shiffman.net">Daniel Shiffman</a>.
4+
*
5+
* Mouse horizontal location controls breaking apart of image and
6+
* Maps pixels from a 2D image into 3D space. Pixel brightness controls
7+
* translation along z axis.
8+
*
9+
* Created 2 May 2005
10+
*/
11+
12+
PImage img; // The source image
13+
int cellsize = 2; // Dimensions of each cell in the grid
14+
int COLS, ROWS; // Number of columns and rows in our system
15+
void setup()
16+
{
17+
size(200, 200, P3D);
18+
img = loadImage("eames.jpg"); // Load the image
19+
COLS = width/cellsize; // Calculate # of columns
20+
ROWS = height/cellsize; // Calculate # of rows
21+
colorMode(RGB,255,255,255,100); // Setting the colormode
22+
}
23+
void draw()
24+
{
25+
background(0);
26+
// Begin loop for columns
27+
for ( int i = 0; i < COLS;i++) {
28+
// Begin loop for rows
29+
for ( int j = 0; j < ROWS;j++) {
30+
int x = i*cellsize + cellsize/2; // x position
31+
int y = j*cellsize + cellsize/2; // y position
32+
int loc = x + y*width; // Pixel array location
33+
color c = img.pixels[loc]; // Grab the color
34+
// Calculate a z position as a function of mouseX and pixel brightness
35+
float z = (mouseX / (float) width) * brightness(img.pixels[loc]) - 100.0f;
36+
// Translate to the location, set fill and stroke, and draw the rect
37+
pushMatrix();
38+
translate(x,y,z);
39+
fill(c);
40+
noStroke();
41+
rectMode(CENTER);
42+
rect(0,0,cellsize,cellsize);
43+
popMatrix();
44+
}
45+
}
46+
}
43.5 KB
Loading
43.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)