/*	********************
    Name: Mr Meyer
    Date: CISC 1600
    Project: 3D using "shapes" and "vertex"
    
    Note: We saw in the previous example how we can create
    the illusion of 3D space on our own, using a handful of
    variables and a formulat for finding the projected x and y
    (pX and pY) for any given point in processing using a simple
    formula. This exercise takes advantage of processings
    native vertex object and it's beginShape() and endShape()
    functions.
 */

/* ********************
  Variables:
    Variables provide a way to save information within your sketch 
    and use it to control size, position, shape, etc. of what
    you are drawing.
 */
float xmag, ymag = 0;
float z=0;
PImage a;
PImage b;

/* ********************
  setup():
    Use setup() to specify things that need to be done once, 
    when the sketch first opens.
 */
void setup() {
  
  size(640, 360, P3D); 
  a = loadImage("five.jpeg");
  b = loadImage("pumpkin.jpg");
  
}

/* ********************
  draw():  
    Use draw() to specify things that you want to have done repeatedly.
    draw() must be present in your program, even if it is left empty.
 */
void draw() {

    background(#000000); // erase the screen
    
    /* These lines of code calculate how much the X and Y planes
	   should be rotated based on the distance of the mouse from
	   upper corner of the sceen
	*/    
          xmag = mouseX/float(width) * TWO_PI;
          ymag = mouseY/float(height) * TWO_PI;
          

	// Finally we draw our cube.		           
    drawCube(xmag, ymag, z);
        


}

/* ********************
  Event Handlers:
    Use event-handlers like keyPressed() to allow users of your program 
    to cause things to happen.
 */
void keyPressed() 
{
  println("pressed " + int(key) + " " + keyCode);
  
  if(keyCode == 38)
  {
    z -= 10;
  }
  
  if(keyCode == 40)
  {
    z += 10;
  }
  
  if(keyCode == 81)
  {
    exit();
  }
}

/* ********************
  Custom Functions:
    Use this section for functions that you create and name.
 */
void drawCube(float xR, float yR, float zD) {
  
  pushMatrix(); // save the state of the world
  
      translate(width/2, height/2, zD);  // We want it drawn in the center of the screen
      rotateX(-yR); 
      rotateY(-xR); 

      beginShape(QUADS);
            
              texture(a);
              textureMode(IMAGE);
              // "five.jpeg" is 200x200 pixels in size so
              // the values 0 and 200 are used for the
              // parameters "u" and "v" to map it directly
              // to the vertex points
              vertex( -50, -50,  50,   0,   0);
              vertex(  50, -50,  50, 200,   0);
              vertex(  50,  50,  50, 200, 200);
              vertex( -50,  50,  50,   0, 200);
              
      endShape();
    
      beginShape(QUADS);
      
              texture(b);
              textureMode(IMAGE);
              // "pumpkin.jpg" is 100x100 pixels in size so
              // the values 0 and 100 are used for the
              // parameters "u" and "v" to map it directly
              // to the vertex points
              
              vertex( 50,  50,  50,   0,   0);
              vertex( 50,  50, -50, 100,   0);
              vertex( 50, -50, -50, 100, 100);
              vertex( 50, -50,  50,   0, 100);
              
      endShape();

      beginShape(QUADS);
                 
              fill(#FF0000); vertex( 50,  50, -50);
              fill(#00FF00); vertex(-50,  50, -50);
              fill(#0000FF); vertex(-50, -50, -50);
              fill(#FFFFFF); vertex( 50, -50, -50);
            
              fill(#FF0000); vertex(-50,  50, -50);
              fill(#0000FF); vertex(-50,  50,  50);
              fill(#00FF00); vertex(-50, -50,  50);
              vertex(-50, -50, -50);
            
              fill(#FF0000); vertex(-50,  50, -50);
              fill(#000000); vertex( 50,  50, -50);
              fill(#0000FF); vertex( 50,  50,  50);
              fill(#00FF00); vertex(-50,  50,  50);
            
              fill(#FF0000); vertex(-50, -50, -50);
              fill(#FFFFFF); vertex( 50, -50, -50);
              fill(#0000FF); vertex( 50, -50,  50);
              fill(#00FF00); vertex(-50, -50,  50);
    
      endShape();
  
  popMatrix();
  
}

/* ********************
  Classes:
    Used to create objects.
*/

Source code: SimpleDie

Built with Processing