diff --git a/java/examples/Basics/Camera/MoveEye/MoveEye.pde b/java/examples/Basics/Camera/MoveEye/MoveEye.pde new file mode 100644 index 000000000..6a6384640 --- /dev/null +++ b/java/examples/Basics/Camera/MoveEye/MoveEye.pde @@ -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); +} diff --git a/java/examples/Basics/Camera/Perspective/Perspective.pde b/java/examples/Basics/Camera/Perspective/Perspective.pde new file mode 100644 index 000000000..46695397d --- /dev/null +++ b/java/examples/Basics/Camera/Perspective/Perspective.pde @@ -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); +} + diff --git a/java/examples/Basics/Lights/Directional/Directional.pde b/java/examples/Basics/Lights/Directional/Directional.pde new file mode 100644 index 000000000..6a1af3008 --- /dev/null +++ b/java/examples/Basics/Lights/Directional/Directional.pde @@ -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); +} + diff --git a/java/examples/Basics/Lights/Lights1/Lights1.pde b/java/examples/Basics/Lights/Lights1/Lights1.pde new file mode 100644 index 000000000..bf7a36edc --- /dev/null +++ b/java/examples/Basics/Lights/Lights1/Lights1.pde @@ -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(); +} diff --git a/java/examples/Basics/Lights/Lights2/Lights2.pde b/java/examples/Basics/Lights/Lights2/Lights2.pde new file mode 100644 index 000000000..e1693e590 --- /dev/null +++ b/java/examples/Basics/Lights/Lights2/Lights2.pde @@ -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); +} diff --git a/java/examples/Basics/Lights/Reflection/Reflection.pde b/java/examples/Basics/Lights/Reflection/Reflection.pde new file mode 100644 index 000000000..f6a3d7a94 --- /dev/null +++ b/java/examples/Basics/Lights/Reflection/Reflection.pde @@ -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); +} diff --git a/java/examples/Basics/Lights/Spot/Spot.pde b/java/examples/Basics/Lights/Spot/Spot.pde new file mode 100644 index 000000000..ce25c4bee --- /dev/null +++ b/java/examples/Basics/Lights/Spot/Spot.pde @@ -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); +} + diff --git a/java/examples/Basics/Transform/TriangleFlower/TriangleFlower.pde b/java/examples/Basics/Transform/TriangleFlower/TriangleFlower.pde deleted file mode 100644 index e21c35a67..000000000 --- a/java/examples/Basics/Transform/TriangleFlower/TriangleFlower.pde +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Triangle Flower - * by Ira Greenberg. - * - * Using rotate() and triangle() functions generate a pretty - * flower. Uncomment the line "// rotate(rot+=radians(spin));" - * in the triBlur() function for a nice variation. - */ - -PVector[] p = new PVector[3]; -float shift = 0.2; -float fade = 0; -float fillCol = 0; -float rot = 0; -float spin = 0; - -void setup() { - size(640, 360); - background(0); - smooth(); - fade = 255.0 / (width*0.5 / shift); - spin = 360.0 / (width*0.5 / shift); - p[0] = new PVector(-width/2, height/2); - p[1] = new PVector(width/2, height/2); - p[2] = new PVector(0, -height/2); - noStroke(); - translate(width/2, height/2); - triBlur(); -} - -void triBlur() { - fill(fillCol); - fillCol += fade; - rotate(spin); - triangle(p[0].x += shift, p[0].y -= shift/2, - p[1].x -= shift, p[1].y -= shift/2, - p[2].x, p[2].y += shift); - if (p[0].x < 0) { - // ecursive call - triBlur(); - } -} -