/* CSC220F19DemoPShapeassn2 demos my custom PShape from assignment 2, csc220 fall 2019 You can test out your custom makeCustomPShape() function for assignment 2 by building it below in makeCustomPShape() to play with it, then copying function makeCustomPShape() back into assignment 2. 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 demoPShape = null ; float rotx = 0.0, roty = 0.0, rotz = 0.0 ; float rotspeed = 1.0 ; void setup() { size(800, 600, P3D); demoPShape = makeCustomPShape(); shapeMode(CENTER); textAlign(LEFT); background(255); } void draw() { // display rotating PShape ortho() ; // easier to see design of PShape than perspective() background(255); fill(0); // for text textSize(20); // points text("base is cyan, left is red, right is green, back is blue", 50, 50); pushMatrix(); translate(width/2, height/2, 0); rotateX(radians(rotx)); rotateY(radians(roty)); rotateZ(radians(rotz)); shape(demoPShape, 0, 0); // translate 0,0,0 point gives center of rotation popMatrix(); keyPoll(); } // D. Parson's makeCustomPShape vectors taken from csc220 fall2019 // assignment 2 CSC220F19Demo3Da /* * Make and return a custom 3D PShape created using vertex() calls, * for use in Avatar-derived class VectorAvatar. The textureimg * parameter may be null; when it is non-null, use it to texture * at least one of the planar sides of the returned PShape. If the * STUDENT decides not to texture, remove the loadImage call at the * top of the sketch, allowing textureimg to be null. */ PShape makeCustomPShape() { // STUDENT NOTE: Even though // https://processing.org/reference/vertex_.html // shows use of 3D coordinates combined with texture: // vertex(x, y, z, u, v), that did not work for my // 3D planar surfaces like the initial base that varies // the Z value. Intuitively, the limitation makes sense, // since texturing images are 2D, and varying the Z // coordinate can create a non-planar shape, even though // mine are all planar. I switched to vertex(x, y, u, v) // for the textured planar surface in the else clause below, // then used rotateX and translate to move it into position // within the GROUP PShape. PShape base = createShape(); base.beginShape(); base.vertex(0,100,100); // vertex(x, y, z) with varying base.vertex(-100,100,-100); // Z did not work for texturing, base.vertex(100,100,-100); // even though the constant Y value base.vertex(0,100,100); // makes this surface planar. base.endShape(); base.setFill(color(0,255,255)); PShape left = createShape(); // Al of the other planar sides use the Z coordinate. left.beginShape(); left.vertex(0,100,100); // center,bottom,front left.vertex(0,-100,0); // center,top, center (pyramid apex) left.vertex(-100,100,-100); // left,bottom,back left.vertex(0,100,100); // close at original vertex for setFill to work left.endShape(); left.setFill(color(255,0,0)); PShape right = createShape(); right.beginShape(); right.vertex(0,100,100); right.vertex(0,-100,0); right.vertex(100,100,-100); right.vertex(0,100,100);; right.endShape(); right.setFill(color(0,255,0)); PShape back = createShape(); back.beginShape(); back.vertex(-100,100,-100); back.vertex(100,100,-100); back.vertex(0,-100,0); back.vertex(-100,100,-100); back.endShape(); back.setFill(color(0,0,255)); PShape custom = createShape(GROUP); custom.addChild(base); custom.addChild(left); custom.addChild(right); custom.addChild(back); custom.translate(100,100,0); // trial-and-error, slide into centered position return custom ; } 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 ; } }