mirror of
https://github.com/processing/processing4.git
synced 2026-05-05 10:25:38 +02:00
Added new P3D examples
This commit is contained in:
182
java/libraries/opengl/examples/Form/Toroid/Toroid.pde
Executable file
182
java/libraries/opengl/examples/Form/Toroid/Toroid.pde
Executable file
@@ -0,0 +1,182 @@
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user