mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
Added updated examples from branch
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* Move Eye.
|
||||
* by Simon Greenwold.
|
||||
*
|
||||
* The camera lifts up (controlled by mouseY) while looking at the same point.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
size(640, 360, P3D);
|
||||
orientation(LANDSCAPE);
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
/**
|
||||
* 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);
|
||||
orientation(LANDSCAPE);
|
||||
|
||||
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(0, width, 0, height, 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* 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);
|
||||
orientation(LANDSCAPE);
|
||||
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(0, width, 0, height, 50, 500);
|
||||
}
|
||||
|
||||
translate(width/2, height/2, 0);
|
||||
rotateX(-PI/6);
|
||||
rotateY(PI/3);
|
||||
box(160);
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* 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);
|
||||
orientation(LANDSCAPE);
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user