import processing.opengl.*;
// HW6
// Put the required comments below:
// Jennifer Shin
// Section D
// jshin1@andrew.cmu.edu
// Copyright © Jennifer Shin March 2011 Pittsburgh, PA 15221 All Rights Reserved
// Rachel Carr
// no one
/***************************************
Flying Around My Initial
Translation Controls:
----------------- [ up arrow ] move up ------------------
-- [left arrow] move left -- move right [right arrow] --
--------------- [ down arrow ] move down ----------------
-- [c] key - to move closer --- to move away - key [a] --
Rotation Controls:
-- Move sliders for x, y and z axis rotation
Other Controls:
-- [s] key or stop button to stop translation
-- [space] key or reset button to return to zero positions
-- [z] to zoom away from initial
-- [x] to return to original position before zoom
***************************************/
// Put Your Global Variables Here:
float x, y, z, oldZ;
float translateX, translateY, translateZ;
float xRotation, yRotation, zRotation;
float xRectX, xRectY, xRectWidth, xRectHeight, xTabX, xTabY, xTabWidth, xTabHeight;
float yRectX, yRectY, yRectWidth, yRectHeight, yTabX, yTabY, yTabWidth, yTabHeight;
float zRectX, zRectY, zRectWidth, zRectHeight, zTabX, zTabY, zTabWidth, zTabHeight;
void setup( )
{
size( 600, 600, OPENGL );
// The default frame rate of 60 is too fast when int values are used
// to control translation and rotation. Using float values for
// movement causes rounding errors that are unpleasant. By slowing
// the frame rate down, movement with int values seems to look better.
// Alter this as you wish but be careful...
frameRate( 30 );
noStroke ( );
translateX = 0;
translateY = 0;
translateZ = 0;
x = 0;
y = 0;
z = 0;
xRectX = width / 6 - ( width / 2 );
xRectY = height / 24 - ( height / 2 );
xRectWidth = width / 3 * 2;
xRectHeight = height * .025;
xTabX = 0;
xTabY = height / 30 - ( height / 2 );
xTabWidth = width / 60;
xTabHeight = height / 24;
yRectX = width *.93 - ( width / 2 );
yRectY = height / 6 - ( height / 2 );
yRectWidth = width *.025;
yRectHeight = height / 3 * 2;
yTabX = width * .4205;
yTabY = 0;
yTabWidth = width / 24;
yTabHeight = height / 60;
zRectX = width / 6 - ( width / 2 );
zRectY = height * .93 - ( width / 2 );
zRectWidth = width / 3 * 2;
zRectHeight = height * .025;
zTabX = 0;
zTabY = height * .9205 - ( height / 2 );
zTabWidth = width / 60;
zTabHeight = height / 24;
}
void draw( )
{
// Alter the background to any value you wish:
background( 0 );
// Call your functions to draw the controls and display the data here
// before any rotations and translations:
buttons ( );
distInitials ( );
rotInitials ( );
speedInitials ( );
// DO NOT ALTER THE NEXT LINE -- THIS SETS THE (O,0,0) POINT AT THE CENTER OF THE WINDOW
translate( width/2, height/2, 0 );
// Call your function to change the translation and rotation variables here :
rotateXAxis ( );
rotateYAxis ( );
rotateZAxis ( );
// If you want to experiment with the lighting functions, call the function here:
lights ( );
// Call the translate( int, int, int) function here:
transInitials ( );
// Call the three rotate functions here:
rotateX ( radians ( xRotation ) );
rotateY ( radians ( yRotation ) );
rotateZ ( radians ( zRotation ) );
// Jim coded the drawAxis() function to guide you in placing your initials.
// You can leave the call in your final version or delete it.
// drawAxis( );
// Call your function to draw your initials here:
drawInitials ( );
}
void keyPressed( )
{
// if ( key == 'j')
// {
// saveFrame("HW7.jpg");
// }
if ( key == CODED )
{
if ( keyCode == UP )
{
translateY = translateY - 1;
}
else if ( keyCode == DOWN )
{
translateY = translateY + 1;
}
else if ( keyCode == LEFT )
{
translateX = translateX - 1;
}
else if ( keyCode == RIGHT )
{
translateX = translateX + 1;
}
}
else if ( key == 'a' )
{
translateZ = translateZ - 1;
}
else if ( key == 'c' )
{
translateZ = translateZ + 1;
}
else if ( key == 's' )
{
translateX = 0;
translateY = 0;
translateZ = 0;
}
else if ( key == ' ' )
{
translateX = 0;
translateY = 0;
translateZ = 0;
x = 0;
y = 0;
z = 0;
}
else if ( key == 'z' )
{
oldZ = z;
z = z - 500;
}
else if ( key == 'x' )
{
z = oldZ;
}
}
void drawAxis( )
{
fill( 255 ); // white horizontal bar x-axis
box( 800, 5, 5 );
fill( 0, 0, 255); // blue vertical bar y-axis
box( 5, 800, 5 );
fill( 255, 0, 0 ); // red bar pointing towards you z-axis
box( 5, 5, 800 );
fill( 0, 255, 0 );
sphere( 20 ); // green sphere at the center of the window
}
void transInitials ( )
{
x = x + translateX;
y = y + translateY;
z = z + translateZ;
}
void rotateXAxis ( )
{
if ( mousePressed )
{
if ( ( mouseX - width / 2 ) >= xRectX && ( mouseX - width / 2 ) < xRectX + xRectWidth &&
( mouseY - height / 2 ) >= xRectY && ( mouseY - height / 2 ) < xRectY + xRectHeight )
{
xTabX = mouseX - width / 2;
}
}
fill ( 0, 255, 0 );
rectMode ( CORNER );
rect ( xRectX, xRectY, xRectWidth, xRectHeight );
fill ( 255, 255, 255 );
rect ( xTabX, xTabY, xTabWidth, xTabHeight );
xRotation = map ( xTabX, -200, 200, -360, 360 );
fill ( 0, 255, 255 );
textSize ( 14 );
text ( "Rotate About X-Axis", -170, -240 );
}
void rotateYAxis ( )
{
if ( mousePressed )
{
if ( ( mouseX - width / 2 ) >= yRectX && ( mouseX - width / 2 ) < yRectX + yRectWidth &&
( mouseY - height / 2 ) >= yRectY && ( mouseY - height / 2 ) < yRectY + yRectHeight )
{
yTabY = mouseY - height / 2;
}
}
fill ( 0, 255, 0 );
rectMode ( CORNER );
rect ( yRectX, yRectY, yRectWidth, yRectHeight );
fill ( 255, 255, 255 );
rect ( yTabX, yTabY, yTabWidth, yTabHeight );
yRotation = map ( yTabY, -200, 200, -360, 360 );
fill ( 0, 255, 255 );
textSize ( 14 );
text ( "Rotate About Y-Axis", 170, -210 );
}
void rotateZAxis ( )
{
if ( mousePressed )
{
if ( ( mouseX - width / 2 ) >= zRectX && ( mouseX - width / 2 ) < zRectX + zRectWidth &&
( mouseY - height / 2 ) >= zRectY && ( mouseY - height / 2 ) < zRectY + zRectHeight )
{
zTabX = mouseX - width / 2;
}
}
fill ( 0, 255, 0 );
rectMode ( CORNER );
rect ( zRectX, zRectY, zRectWidth, zRectHeight );
fill ( 255, 255, 255 );
rect ( zTabX, zTabY, zTabWidth, zTabHeight );
zRotation = map ( zTabX, zRectX, zRectX + zRectWidth, -360, 360 );
fill ( 0, 255, 255 );
textSize ( 14 );
text ( "Rotate About Z-Axis", -170, 290 );
}
void buttons ( )
{
fill ( 255, 0, 0 );
ellipseMode ( CORNER );
ellipse ( 25, 25, 50, 50 );
fill ( 0 );
textSize ( 14 );
text ( "STOP", 32.5, 45, 40, 40 );
fill ( 0, 255, 255 );
ellipseMode ( CORNER );
ellipse ( 25, 100, 50, 50 );
fill ( 0 );
textSize ( 12 );
text ( "RESET", 30, 120, 40, 40 );
if ( mousePressed )
{
if ( mouseX >= 25 && mouseX <= 75 && mouseY >= 25 && mouseY <= 75 )
{
translateX = 0;
translateX = translateX;
translateY = 0;
translateY = translateY;
translateZ = 0;
translateZ = translateZ;
}
else if ( mouseX >= 25 && mouseX <= 75 && mouseY >= 100 && mouseY <= 150 )
{
translateX = 0;
translateY = 0;
translateZ = 0;
x = 0;
y = 0;
z = 0;
xTabX = 0;
yTabY = 0;
zTabX = 0;
}
}
}
void distInitials ( )
{
textSize ( 13 );
fill ( 0, 255, 255 );
text ( "Distance from: ", 15, 165 );
text ( "X: " + int ( x ), 15, 180 );
text ( "Y: " + int ( y ), 15, 195 );
text ( "Z: " + int ( z ), 15, 210 );
}
void rotInitials ( )
{
textSize ( 13 );
fill ( 255, 255, 0 );
text ( "Rotation About: ", 15, 225 );
text ( "X-Axis: " + int ( xRotation ), 15, 240 );
text ( "Y-Axis: " + int ( yRotation ), 15, 255 );
text ( "Z-Axis: " + int ( zRotation ), 15, 270 );
}
void speedInitials ( )
{
textSize ( 13 );
fill ( 255, 0, 255 );
text ( "Speed: ", 15, 285 );
text ( "X: " + int ( translateX ), 15, 300 );
text ( "Y: " + int ( translateY ), 15, 315 );
text ( "Z: " + int ( translateZ ), 15, 330 );
}
void drawInitials ( )
{
translate ( x, y, z );
pushMatrix ( );
translate ( width * .2, height * -.15, width * .1 );
fill ( 255, 0, 30 );
sphere ( width * .025 );
popMatrix ( );
pushMatrix ( );
translate ( width * .2, height * -.15, width * .1 );
fill ( 255, 150 );
sphere ( width * .05 );
popMatrix ( );
pushMatrix ( );
translate ( width * .15, height * -.3, width * .05);
fill ( 250, 232, 25 );
box ( width * .04 );
popMatrix ( );
pushMatrix ( );
translate ( width * .15, height * -.3, width * .05 );
fill ( 255, 150 );
box ( width * .08 );
popMatrix ( );
pushMatrix ( );
translate ( 0, height * -.35, 0 );
fill ( 250, 108, 25 );
box ( width * .04 );
popMatrix ( );
pushMatrix ( );
translate ( 0, height * -.35, 0 );
fill ( 255, 150 );
box ( width * .08 );
popMatrix ( );
pushMatrix ( );
translate ( width * -.15, height * -.30, width * -.10 );
fill ( 247, 5, 244 );
box ( width * .04 );
popMatrix ( );
pushMatrix ( );
translate ( width * -.15, height * -.30, width * -.10 );
fill ( 255, 150 );
box ( width * .08 );
popMatrix ( );
pushMatrix ( );
translate ( width * -.25, height * -.15, width * -.15 );
fill ( 250, 232, 2 );
box ( width * .04 );
popMatrix ( );
pushMatrix ( );
translate ( width * -.25, height * -.15, width * -.15 );
fill ( 255, 150 );
box ( width * .08 );
popMatrix ( );
pushMatrix ( );
translate ( width * -.15, height * -.02, width * -.2 );
fill ( 250, 108, 25 );
box ( width * .04 );
popMatrix ( );
pushMatrix ( );
translate ( width * -.15, height * -.02, width * -.2 );
fill ( 255, 150 );
box ( width * .08 );
popMatrix ( );
pushMatrix ( );
translate ( 0, height * .025, width * -.25 );
fill ( 247, 5, 244 );
box ( width * .04 );
popMatrix ( );
pushMatrix ( );
translate ( 0, height * .025, width * -.25 );
fill ( 255, 150 );
box ( width * .08 );
popMatrix ( );
pushMatrix ( );
translate ( width * .15, height * .07, width * -.2 );
fill ( 250, 232, 25 );
box ( width * .04 );
popMatrix ( );
pushMatrix ( );
translate ( width * .15, height * .07, width * -.2 );
fill ( 255, 150 );
box ( width * .08 );
popMatrix ( );
pushMatrix ( );
translate ( width * .25, height * .20, width * -.15 );
fill ( 250, 108, 25 );
box ( width * .04 );
popMatrix ( );
pushMatrix ( );
translate ( width * .25, height * .20, width * -.15 );
fill ( 255, 150 );
box ( width * .08 );
popMatrix ( );
pushMatrix ( );
translate ( width * .15, height * .35, width * -.10 );
fill ( 247, 5, 244 );
box ( width * .04 );
popMatrix ( );
pushMatrix ( );
translate ( width * .15, height * .35, width * -.10 );
fill ( 255, 150 );
box ( width * .08 );
popMatrix ( );
pushMatrix ( );
translate ( 0, height * .40, width * -.05 );
fill ( 250, 232, 25 );
box ( width * .04 );
popMatrix ( );
pushMatrix ( );
translate ( 0, height * .40, width * -.05 );
fill ( 255, 150 );
box ( width * .08 );
popMatrix ( );
pushMatrix ( );
translate ( width * -.15, height * .35, 0 );
fill ( 250, 108, 25 );
box ( width * .04 );
popMatrix ( );
pushMatrix ( );
translate ( width * -.15, height * .35, 0 );
fill ( 255, 150 );
box ( width * .08 );
popMatrix ( );
pushMatrix ( );
translate ( width * -.30, height * .25, width * .05 );
fill ( 247, 5, 244 );
box ( width * .04 );
popMatrix ( );
pushMatrix ( );
translate ( width * -.30, height * .25, width * .05 );
fill ( 255, 150 );
box ( width * .08 );
popMatrix ( );
pushMatrix ( );
translate ( width * -.30, height * .10, width * .10 );
fill ( 255, 0, 30 );
sphere ( width * .025 );
popMatrix ( );
pushMatrix ( );
translate ( width * -.30, height * .10, width * .10 );
fill ( 255, 150 );
sphere ( width * .05 );
popMatrix ( );
}