/* CSC220F19DemoPShapeCylinder demos a cylinder supplied by a student solution for assignment 2, csc220 fall 2019. D. Parson, Fall 2019, CSC220 KEYBOARD COMMANDS: LEFT and RIGHT arrows rotate PShape around Y axis. UP and DOWN arrows rotate PShape around X axis. 'o' and 'O' keys rotate PShape around Z axis. 'R' resets rotations to 0, 0, 0 IF A KEY STICKS, HIT SPACE BAR TO UNSTICK IT. */ PShape demoCylinder = null ; float rotx = 0.0, roty = 0.0, rotz = 0.0 ; float rotspeed = 1.0 ; // Parameters for the demo cylinder: final int cradius = 80, cheight = 200, cdetail = 32 ; void setup() { size(800, 600, P3D); frameRate(60); // Must be explicit in newer Macs after setting size. demoCylinder = makeCylinder(cradius, cheight, cdetail, 0, 255, 255); shapeMode(CENTER); textAlign(LEFT); background(255); } void draw() { // display rotating PShape ortho() ; // easier to see design of PShape than perspective() background(255); pushMatrix(); translate(width/2, height/2, 0); // Draw a bounding box using lines around the cylinder to ensure correct center of it. line(-cradius, -height/2, -cradius, height/2); line(cradius, -height/2, cradius, height/2); line(-width/2, -cheight/2, width/2, -cheight/2); line(-width/2, cheight/2, width/2, cheight/2); rotateX(radians(rotx)); rotateY(radians(roty)); rotateZ(radians(rotz)); stroke(0); demoCylinder.setFill(color(255, 255, 0)); // These override defaults for the PShape. demoCylinder.setStroke(color(0,255,255)); demoCylinder.setStrokeWeight(2); shape(demoCylinder, 0, 0); // translate 0,0,0 point gives center of rotation popMatrix(); keyPoll(); } PShape makeCylinder(float radius, float height, int detail, int Red, int Green, int Blue) { textureMode(NORMAL); PShape cyl = createShape(); cyl.beginShape(QUAD_STRIP); cyl.stroke(0); cyl.strokeWeight(1); cyl.fill(Red, Green, Blue); float angle = TWO_PI / detail; for (int i = 0; i <= detail; i++) { float x = sin(i * angle); float z = cos(i * angle); float u = float(i) / detail; cyl.normal(x, 0, z); cyl.vertex(x * radius, -height/2, z * radius, u, 0); cyl.vertex(x * radius, +height/2, z * radius, u, 1); } cyl.endShape(); cyl.translate(radius, height/2, 0); // Above code has it offset to LEFT and UP. return cyl; } void keyPressed() { if (key == 'R') { rotx = roty = rotz = 0.0 ; } } void keyPoll() { if (key == CODED) { if (keyCode == UP) { rotx = (rotx + rotspeed) % 360 ; } else if (keyCode == DOWN) { rotx = (rotx + 360 - rotspeed) % 360 ; } else if (keyCode == LEFT) { roty = (roty + 360 - rotspeed) % 360 ; } else if (keyCode == RIGHT) { roty = (roty + rotspeed) % 360 ; } } else { } if (key == 'O') { rotz = (rotz + 360 - rotspeed) % 360 ; } else if (key == 'o') { rotz = (rotz + rotspeed) % 360 ; } }