08.01.2015 Views

Computer Graphics

Computer Graphics

Computer Graphics

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Computer</strong> <strong>Graphics</strong><br />

Transformation & Animation


Transformations


(1, 2)


(1, 2) (2, 2)


(2, 3)<br />

(1, 2) (2, 2)


(1, 3) (2, 3)<br />

(1, 2) (2, 2)


16 Points!!


Cube<br />

drawCube();


drawCube();<br />

// Move right 2 squares<br />

Cube


drawCube();<br />

// Move right 2 squares<br />

drawCube();<br />

Cube<br />

Cube


Cube<br />

Cube


Cube<br />

Cube<br />

Cube


Cube<br />

Cube<br />

Cube


Cube<br />

Cube<br />

Cube<br />

Cube


Same cube<br />

Different origin<br />

Cube<br />

Cube<br />

Cube<br />

Cube


Transformations: Types


T<br />

glTranslatef(x, y, z);<br />

Types: Translation


R<br />

glRotatef(angle, x, y, z);<br />

Types: Rotation


S<br />

glScalef(x, y, z);<br />

Types: Scale


Transformations: Matrix Stacks


MV =<br />

I<br />

Matrix Stacks: Model View


MV = I C ube<br />

<br />

Matrix Stacks: Model View


MV = I C ube<br />

<br />

T<br />

Matrix Stacks: Model View


MV = I C ube<br />

<br />

T<br />

C ube<br />

<br />

Matrix Stacks: Model View


MV = I C ube<br />

<br />

T<br />

T<br />

C ube<br />

<br />

Matrix Stacks: Model View


MV = I C ube<br />

<br />

T<br />

T<br />

R<br />

C ube<br />

<br />

Matrix Stacks: Model View


MV = I C ube<br />

<br />

T<br />

C ube<br />

<br />

T R C ube<br />

<br />

Matrix Stacks: Model View


Making a mess


Need a way to reset<br />

Cube<br />

Cube<br />

Cube<br />

Cube


glPushMatrix();<br />

drawCube();<br />

Cube


glPushMatrix();<br />

// Draw Cubes<br />

Cube


glPushMatrix();<br />

// Draw Cubes<br />

Cube<br />

Cube


glPushMatrix();<br />

// Draw Cubes<br />

Cube<br />

Cube


glPushMatrix();<br />

// Draw Cubes<br />

Cube<br />

Cube<br />

Cube


glPushMatrix();<br />

// Draw Cubes<br />

Cube<br />

Cube<br />

Cube


glPushMatrix();<br />

// Draw Cubes<br />

Cube<br />

Cube<br />

Cube<br />

Cube


glPushMatrix();<br />

// Draw Cubes<br />

glPopMatrix();<br />

Cube<br />

Cube<br />

Cube<br />

Cube


No unsightly transforms


Transformations: Matrix Stacks


Pro =<br />

I<br />

Matrix Stacks: Projection


Pro = I 2D<br />

gluOrtho2D(…);<br />

y<br />

(0,0)<br />

x<br />

Matrix Stacks: Projection


Pro = I 3D<br />

gluOrtho3D(…);<br />

y<br />

(0,0,0)<br />

x<br />

Matrix Stacks: Projection


Pro = I Per<br />

gluPerspective(…);<br />

y<br />

(0,0,0)<br />

x<br />

Matrix Stacks: Projection


Now Loading<br />

Projection


Animation


T=0 T=1 T=2 T=3 T=4<br />

Animation


glutTimerFunc(…);<br />

T=0 T=1 T=2 T=3 T=4<br />

Animation


Now Loading<br />

Rotating Cube


Animation: User Input


User Input: Keyboard<br />

glutKeyboardFunc(…);


void Render::keyPos(unsigned char key, int x, int y) <br />

{ <br />

switch (key) <br />

{ <br />

case 'r': // Reverse rota=on <br />

// Reverse the rota=on of the planets … (Code here) <br />

break; <br />

case 'p': // Pause rota=on <br />

// Pause the rota=on of the planets … (Code here) <br />

break; <br />

case 13: // 'Enter' Key <br />

showTitle = false; <br />

reshape(Wx, Wy); // Force a reshape so that the new projec=on matrix is loaded <br />

break; <br />

case 27: // 'Esc' key on the keyboard. Quit the program when the esc key is pressed <br />

exit (0); <br />

break; <br />

default: // Unknown key ... do nothing <br />

break; <br />

} <br />

}


glutMouseFunc(…);<br />

glutMotionFunc(…);<br />

User Input: Mouse


OpenGL<br />

2D<br />

y<br />

(0,0)<br />

(Wx,Wy)<br />

x<br />

3D<br />

z<br />

y<br />

(0,0,0)<br />

x<br />

User Input: Mouse


OpenGL<br />

Mouse<br />

y<br />

(Wx,Wy)<br />

2D<br />

(0,0)<br />

x<br />

(0,0)<br />

x<br />

3D<br />

y<br />

x<br />

y<br />

(Wx,Wy)<br />

z<br />

(0,0,0)<br />

User Input: Mouse


void Render::mouseClick(int buVon, int state, int x, int y){ <br />

if (state == GLUT_DOWN) <br />

{ <br />

mOldX = x; <br />

mOldY = y; <br />

switch (buVon) <br />

{ <br />

case GLUT_LEFT_BUTTON: <br />

mBuVon = LEFT; <br />

break; <br />

case GLUT_MIDDLE_BUTTON: <br />

mBuVon = MIDDLE; <br />

break; <br />

case GLUT_RIGHT_BUTTON: <br />

mBuVon = RIGHT; <br />

break; <br />

default: <br />

// Unknown buVon … skip it <br />

break; <br />

} <br />

} <br />

else if (state == GLUT_UP) // If this is the release then reset the buVon <br />

{ <br />

mBuVon = -­‐1; <br />

}}


void Render::mouseMove(int x, int y){ <br />

if (mBuVon == LEFT) <br />

{ <br />

rot[0] -­‐= ((mOldY -­‐ y) * 180.0f) / 1000.0f; <br />

rot[1] -­‐= ((mOldX -­‐ x) * 180.0f) / 1000.0f; <br />

clamp(rot[0], rot[1], rot[2]); <br />

} <br />

else if (mBuVon == MIDDLE) <br />

{ <br />

eye[2] -­‐= ((mOldY -­‐ y) * 180.0f) / 500.0f; <br />

clamp(rot[0], rot[1], rot[2]); <br />

} <br />

else if (mBuVon == RIGHT) <br />

{ <br />

eye[0] += ((mOldX -­‐ x) * 180.0f) / 1000.0f; <br />

eye[1] -­‐= ((mOldY -­‐ y) * 180.0f) / 1000.0f; <br />

clamp(rot[0], rot[1], rot[2]); <br />

} <br />

mOldX = x; <br />

mOldY = y; <br />

// I changed values to be used in the modelview matrix. <br />

// Update my display and the objects on the screen <br />

glutPostRedisplay();}


Whew……


Assignment


Project<br />

• Talk to me about your idea: Today<br />

• Give a short (1 slide) 3 min overview of<br />

what you are doing: Thursday


Questions<br />

Transformation & Animation


Templates


Now Loading<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!