Title: Computer Graphics 4
1Computer Graphics 4
2Introduction to transformations
- Transformations are useful for
- Composing a more complex object using many
instances of a single form. - Creating a complex object from a single "motif".
- Moving an object to get a different view of it.
- Moving an object to animate it.
"motif"
3Mathematics notation
- in 3D, let P (Px, Py, Pz, 1)
- P can be transformed into Q (Qx, Qy, Qz, 1) by
applying a transformation T - Q T(P)
- There are an infinite number of possible
transformations - Some transformations are rather simple
- Some transformations are rather complex
- Some transformations distort the shape of an
object - Some transformations maintain the relative shape
of an object
4Affine transformations
- There is a special class of transformations that
maintain the relative shape of objects (the
affine transforms). - All affine transformations are linear. Therefore,
they can be written as - Qx m11Px m12Py m13Pz m14
- Qy m21Px m22Py m23Pz m24
- Qz m31Px m32Py m33Pz m34
- 1 1
5Definition of affine transformations
- T is an affine transformation if
- T(aP (1-a)Q) aT(P) (1-a)T(Q)
- aP (1-a)Q is the parametric equation of a line
segment. The definition says "if we can take the
line formed by P and Q and transform it, and then
if we can get the same line by transforming only
P and Q and then forming the line,T is an affine
transformation.
6Standard affine transformations
- Translation
- Scaling
- Rotation
- Shearing
7Translation
- a displacement
- a move in a linear direction
- it is not affected by the location of the origin
8Scaling
- to enlarge (or shrink)
- a point at the origin is never effected by
scaling - objects always scale about the origin
9Scaling
- if an object is not centered about the origin,
scaling also moves it - uniform scaling (Sx Sy Sz)
- differential scaling (Sx ? Sy or Sx ? Sz or Sy ?
Sz) - a negative scale factor reflects (flips) about an
axis - -Sx reflects about the Y axis
- -Sy reflects about the X axis
- -Sx and -Sy reflects about the line (x-y)
10Rotation
- to move in a circular direction
- to rotate about the origin
- the angle is always considered a
counter-clockwise rotation. - derivation
- Qx r cos(??)
- r (cos(?)cos(?) - sin(?)sin(?))
- r cos(?)cos(?) - r sin(?)sin(?)
- Px cos(?) - Py sin(?)
- Qy r sin(??)
- r (sin(?)cos(?) cos(?)sin(?))
- r sin(?)cos(?) r cos(?)sin(?)
- Px sin(?) Py cos(?)
11Rotation
- Rotation about Z
- Rotation about Y
- Rotation about X
- where c cos(angle), s sin(angle)
12Rotation
- In 3D, there are an infinite number of axes about
which to rotate. - It can be shown that any rotation about any
arbitrary axis can be decomposed into a series of
"elementary rotations" about the coordinate axes. - We assume the convention that all rotations
follow the right-hand rule "put your thumb along
the axis of rotation and your fingers curl in the
positive angle direction
13Shearing
- to shear in x (Px hPy, Py, Pz) slant
vertically - to shear in y (Px, gPxPy, Pz) slant
horizontally
14lab04.cpp
include ltstdio.hgt include ltstdlib.hgt include
ltGL/glut.hgt define X_AXIS 0 define
Y_AXIS 1 define Z_AXIS 2 GLfloat
size2.0 GLint N4, translateAxis0 GLfloat
xTranslate0.0, yTranslate0.0, zTranslate0.0
GLfloat translateDelta0.1 void
myDraw(void) glColor3f(0.60, .40, .70)
glBegin(GL_POLYGON) glVertex2f(0.,
0.) glVertex2f(1., 0.) glVertex2f(1.,
1.) glVertex2f(0., 1.) glEnd()
void myAxis(void) int i glColor3f(0.98, .04,
.70) glBegin(GL_LINES) for(i1 iltN i)
glVertex2f(-size2.0isize/N,
-size) glVertex2f(-size2.0isize/N,
size) glVertex2f(-size, -size2.0isize/N)
glVertex2f(size, -size2.0isize/N) glEnd()
void myDisplay(void) glClear(GL_COLOR_BUFFER
_BIT) glLoadIdentity() myAxis() glTranslatef
(xTranslate, yTranslate, zTranslate) myDraw()
glFlush() glutSwapBuffers()
15lab04.cpp
void myReshape(int width, int height) glClearCo
lor (.75, .75, .75, 0.0) glViewport(0,0,width,he
ight) glMatrixMode(GL_PROJECTION) glLoadIdenti
ty() glOrtho(-size, size, -size, size, -size,
size) glMatrixMode(GL_MODELVIEW) void
myMouse(int btn, int state, int x, int
y) if(btnGLUT_LEFT_BUTTON state
GLUT_DOWN) if(translateAxis X_AXIS)
xTranslate-translateDelta else
if(translateAxis Y_AXIS) yTranslate-transl
ateDelta else zTranslate-translateDelta e
lse if(btnGLUT_RIGHT_BUTTON state
GLUT_DOWN) if(translateAxis X_AXIS)
xTranslatetranslateDelta
else if(translateAxis Y_AXIS)
yTranslatetranslateDelta else
zTranslatetranslateDelta else
if(btnGLUT_MIDDLE_BUTTON state
GLUT_DOWN) translateAxis if(translateAxis
3) translateAxis0 else return myDisplay(
) void main(int argc, char
argv) glutInit(argc,argv) glutInitDisplayMod
e (GLUT_DOUBLE GLUT_RGB) glutInitWindowSize(50
0,500) glutInitWindowPosition(0,0) glutCreateW
indow("lab04") glutReshapeFunc(myReshape) glut
DisplayFunc(myDisplay) glutMouseFunc(myMouse)
glutMainLoop()
16glLoadIdentity
- The glLoadIdentity function replaces the current
matrix with the identity matrix. - void glLoadIdentity( void )
- Remarks
- The glLoadIdentity function replaces the current
matrix with the identity matrix. It is
semantically equivalent to calling glLoadMatrix
with the identity matrix but in some cases it is
more efficient.
17glTranslated,f
- The glTranslated and glTranslatef functions
multiply the current matrix by a translation
matrix. - void glTranslated( GLdouble x, GLdouble y,
GLdouble z ) - void glTranslatef( GLfloat x, GLfloat y, GLfloat
z ) - Parameters
- x, y, z The x, y, and z coordinates of a
translation vector. - Remarks
- The glTranslate function moves the coordinate
system origin to the point specified by (x, y, z).
18Exercise
- From lab04
- Translate rectangular using keyboard event
- x translate -translateDelta on X axis
- X translate translateDelta on X axis
- y translate -translateDelta on Y axis
- Y translate translateDelta on Y axis
- z translate -translateDelta on Z axis
- Z translate translateDelta on Z axis
19lab05.cpp
include ltstdio.hgt include ltstdlib.hgt include
ltGL/glut.hgt GLint N4 GLfloat
size2.0 GLfloat zTheta30. void
myAxis(void) int i glColor3f(0.98, .04,
.70) glBegin(GL_LINES) for(i1 iltN i)
glVertex2f(-size2.0isize/N,
-size) glVertex2f(-size2.0isize/N,
size) glVertex2f(-size, -size2.0isize/N)
glVertex2f(size, -size2.0isize/N) glEnd()
void myDraw(void) glColor3f(0.98, .04, .70)
glBegin(GL_POLYGON) glVertex3f(0., 0.,
0.) glVertex3f(1., 0., 0.) glVertex3f(1., 1.,
0.) glVertex3f(0., 1., 0.) glEnd() glColor3f
(1., 0., 0.) glBegin(GL_LINE_LOOP) glVertex3f
(0., 0., 0.) glVertex3f(0., 1.,
0.) glVertex3f(0., 1., 1.) glVertex3f(0., 0.,
1.) glEnd() glColor3f(0., 1., 0.)
glBegin(GL_LINE_LOOP) glVertex3f(1., 0.,
0.) glVertex3f(1., 1., 0.) glVertex3f(1., 1.,
1.) glVertex3f(1., 0., 1.) glEnd() glColor3f
(0., 0., 1.)
20lab05.cpp
glBegin(GL_LINE_LOOP) glVertex3f(0., 0.,
0.) glVertex3f(1., 0., 0.) glVertex3f(1., 0.,
1.) glVertex3f(0., 0., 1.) glEnd() glColor3f
(1., 1., 0.) glBegin(GL_LINE_LOOP) glVertex3f
(0., 1., 0.) glVertex3f(1., 1.,
0.) glVertex3f(1., 1., 1.) glVertex3f(0., 1.,
1.) glEnd() void myDisplay(void) glClear(G
L_COLOR_BUFFER_BIT) glLoadIdentity() myAxis()
glRotatef(zTheta, 0.0, 0.0, 1.0) myDraw() gl
Flush() glutSwapBuffers()
void myReshape(int width, int height) glClearCo
lor (.75, .75, .75, 0.0) glViewport(0,0,width,he
ight) glMatrixMode(GL_PROJECTION) glLoadIdenti
ty() glOrtho(-size, size, -size, size, -size,
size) glMatrixMode(GL_MODELVIEW) void
main(int argc, char argv) glutInit(argc,argv
) glutInitDisplayMode (GLUT_DOUBLE
GLUT_RGB) glutInitWindowSize(500,500) glutInit
WindowPosition(0,0) glutCreateWindow("lab05")
glutReshapeFunc(myReshape) glutDisplayFunc(myDis
play) glutMainLoop()
21glRotated,f
- The glRotated and glRotatef functions multiply
the current matrix by a rotation matrix. - void glRotated( GLdouble angle, GLdouble x,
GLdouble y, GLdouble z ) - void glRotatef( GLfloat angle, GLfloat x,
GLfloat y, GLfloat z ) - Parameters
- angle The angle of rotation, in degrees.
- x, y, z The x, y, and z coordinates of a
vector, respectively. - Remarks
- The glRotate function computes a matrix that
performs a counterclockwise rotation of angle
degrees about the vector from the origin through
the point (x, y, z).
22lab0501.cpp
GLfloat xTheta0., yTheta0., zTheta0.,
thetaDelta.01 void myDisplay(void) glClear(G
L_COLOR_BUFFER_BIT) glLoadIdentity() myAxis()
glRotatef(xTheta, 1.0, 0.0, 0.0) glRotatef(yTh
eta, 0.0, 1.0, 0.0) glRotatef(zTheta, 0.0, 0.0,
1.0) myDraw() glFlush() glutSwapBuffers()
void myKeyboard(unsigned char theKey, int x, int
y) switch (theKey) case 'x' xTheta -
thetaDelta break case 'X' xTheta
thetaDelta break case 'y' yTheta -
thetaDelta break case 'Y' yTheta
thetaDelta break case 'z' zTheta -
thetaDelta break case 'Z' zTheta
thetaDelta break case 27 exit(-1) // esc
key myDisplay() glutKeyboardFunc(myKeyboa
rd) // in main()
23lab0502.cpp
GLfloat xTheta0., yTheta0., zTheta0.,
thetaDelta.01 void myIdle() xThetathetaDel
ta yThetathetaDelta zThetathetaDelta myD
isplay() // glutPostRedisplay() glutIdleFunc
(myIdle) // in main()
24glutIdleFunc
- glutIdleFunc sets the global idle callback.
- void glutIdleFunc(void (func)(void))
- Description
- glutIdleFunc sets the global idle callback to be
func so a GLUT program can perform background
processing tasks or continuous animation when
window system events are not being received. If
enabled, the idle callback is continuously called
when events are not being received.
25glutPostRedisplay
- glutPostRedisplay marks the current window as
needing to be redisplayed. - void glutPostRedisplay(void)
- Description
- Mark the normal plane of current window as
needing to be redisplayed. The next iteration
through glutMainLoop, the windows display
callback will be called to redisplay the windows
normal plane. Multiple calls to glutPostRedisplay
before the next display callback opportunity
generates only a single redisplay callback.
glutPostRedisplay may be called within a windows
display or overlay display callback to remark
that window for redisplay.
26Exercise
- From lab05
- Rotate rectangular using mouse event
- Reference lab04
27lab06.cpp
include ltstdio.hgt include ltstdlib.hgt include
ltGL/glut.hgt GLfloat size2.0 GLint N4 GLfloa
t xScale1.5 void myAxis(void) int i glColo
r3f(0.98, .04, .70) glBegin(GL_LINES) for(i1
iltN i) glVertex2f(-size2.0isize/N,
-size) glVertex2f(-size2.0isize/N,
size) glVertex2f(-size, -size2.0isize/N)
glVertex2f(size, -size2.0isize/N) glEnd()
void myDraw(void) glColor3f(0.60, .40, .70)
glBegin(GL_POLYGON) glVertex2f(0.,
0.) glVertex2f(1., 0.) glVertex2f(1.,
1.) glVertex2f(0., 1.) glEnd() void
myDisplay(void) glClear(GL_COLOR_BUFFER_BIT)
glLoadIdentity() myAxis() glScalef(xScale,
1.0, 1.0) myDraw() glFlush() glutSwapBuffers
()
28lab06.cpp
void myReshape(int width, int height) glClearCo
lor (.75, .75, .75, 0.0) glViewport(0,0,width,he
ight) glMatrixMode(GL_PROJECTION) glLoadIdenti
ty() glOrtho(-size, size, -size, size, -size,
size) glMatrixMode(GL_MODELVIEW) void
main(int argc, char argv) glutInit(argc,argv
) glutInitDisplayMode (GLUT_DOUBLE
GLUT_RGB) glutInitWindowSize(500,500) glutInit
WindowPosition(0,0) glutCreateWindow("lab06")
glutReshapeFunc(myReshape) glutDisplayFunc(myDis
play) glutMainLoop()
29glScaled,f
- The glScaled and glScalef functions multiply the
current matrix by a general scaling matrix. - void glScaled( GLdouble x, GLdouble y, GLdouble z
) void glScalef( GLfloat x, GLfloat y, GLfloat z
) - Parameters
- x, y, z Scale factors along the x, y, and z
axes, respectively. - Remarks
- The glScale function produces a general scaling
along the x, y, and z axes. The three arguments
indicate the desired scale factors along each of
the three axes.
30lab0601.cpp
Glfloat xScale1.0, yScale1.0, zScale1.0,
scaleFactor1.1 void myDraw(void) glColor3f(0
.60, .40, .70) glBegin(GL_POLYGON) glVertex2f
(0., 0.) glVertex2f(1., 0.) glVertex2f(1.,
1.) glVertex2f(0., 1.) glEnd() void
myDisplay(void) glClear(GL_COLOR_BUFFER_BIT)
glLoadIdentity() myAxis() glScalef(xScale,
yScale, zScale) myDraw() glFlush() glutSwapB
uffers()
void myKeyboard(unsigned char theKey, int x, int
y) switch (theKey) case 'x' xScale /
scaleFactor break case 'X' xScale
scaleFactor break case 'y' yScale /
scaleFactor break case 'Y' yScale
scaleFactor break case 'z' zScale /
scaleFactor break case 'Z' zScale
scaleFactor break case 27 exit(-1) // esc
key myDisplay() glutKeyboardFunc(myKeyboa
rd) // in main()
31Homework 3.
- In two weeks
- From lab05
- Rotate rectangle about origin
- Using glutMouseFunc, glutMotionFunc
- Rotation angle(xTheta, yTheta) depend on the
mouse movement while left mouse buttons are
pressed
32(No Transcript)