Camera Analogy and Transformations• Viewing transformations – tripod–define position and orientation of the viewing volume in the world... Coordinate Systems and Transformations• Steps
Trang 1Transformations
Trang 2What is a Transformation?
• Maps points (x, y) in one coordinate system to
points (x', y') in another coordinate system
x' = ax+ by + c y' = dx+ ey+ f
• Simple Transformations
– can be combined
Trang 3Transformations are used
• Position objects in a scene (modeling)
• Change the shape of objects
• Create multiple copies of objects
• Projection for virtual cameras
• Animations
Trang 4How are Transforms Represented?
Biểu diễn dưới dạng ma trận
p' = M p + t
•
Trang 52D transformations
Trang 6Affine Transformations
• Want transformations which preserve geometry
– lines, polygons, quadrics
• Affine = line preserving
– Rotation, translation, scaling
– Projection
– Concatenation (composition)
Trang 8(Nonuniform) Scale
• Scale
S = S-1 =
•
Trang 9•
Trang 11Composing Transforms
• Often want to combine transforms
• E.g first scale by 2, then rotate by 45 degrees
• Advantage of matrix formulation: All still a matrix
• Not commutative!! Order matters
– X2 = SX1
– X3 = RX2
– X3 = R(SX1) = (RS)X1
– X (SR)X
Trang 12Inverting Composite Transforms
• Say I want to invert a combination of 3
transforms
• Option 1: Find composite matrix, invert
• Option 2: Invert each transform and swap order
• Obvious from properties of matrices
Trang 133D rotations
Trang 15Rotations in 3D
• Rotations about coordinate axes simple
• Always linear, orthogonal
Trang 16Geometric Interpretation 3D Rotations
• Rows of matrix are 3 unit vectors of new coord frame
• Can construct rotation matrix from 3 orthonormal
Trang 17Geometric Interpretation 3D Rotations
• Rows of matrix are 3 unit vectors of new coord frame
• Can construct rotation matrix from 3 orthonormal vectors
• Effectively, projections of point into new coord frame
• New coord frame uvw taken to cartesian components xyz
• Inverse or transpose takes xyz cartesian to uvw
Trang 18• Not Commutative (unlike in 2D)!!
• Rotate by x, then y is not same as y then x
• Order of applying rotations does matter
• Follows from matrix multiplication not
commutative
– R1 * R2 is not the same as R2 * R1
• Demo: HW1, order of right or up will matter
Trang 19Homogeneous Coordinates
Trang 20• E.g move x by + 5 units, leave y, z unchanged
• We need appropriate matrix What is it?
Trang 21Homogeneous Coordinates
• Add a fourth homogeneous coordinate (w=1)
• 4x4 matrices very common in graphics, hardware
• Last row always 0 0 0 1 (until next lecture)
Trang 22Representation of Points (4-Vectors)
For w > 0, normal finite point
For w = 0, point at infinity (used for vectors to stop
Trang 23Advantages of Homogeneous Coords
• Unified framework for translation, viewing, rot…
• Can concatenate any set of transforms to 4x4
matrix
• No division (as for perspective viewing) till end
• Simpler formulas, no special cases
• Standard in graphics software, hardware
Trang 24General Translation Matrix
Trang 25Combining Translations, Rotations
• Order matters!! TR is not the same as RT
• General form for rigid body transforms
• We show rotation first, then translation
(commonly used to position objects) on next
slide Slide after that works it out the other way
Trang 26Combining Translations, Rotations
x y z
Trang 27Combining Translations, Rotations
Trang 28Transformations in OpenGL
Trang 31Camera Analogy and Transformations
• Viewing transformations
– tripod–define position and
orientation of the viewing
volume in the world
Trang 32Coordinate Systems and Transformations
• Steps in Forming an Image
– Specify geometry (world coordinates)
– Specify camera (camera coordinates)
– Project (window coordinates)
– Map to viewport (screen coordinates)
• Each step uses transformations
• Every transformation is equivalent to a change
in coordinate systems (frames)
Trang 33Homogeneous Coordinates
• Each vertex has an extra value, w
• Most of the time w = 1, and we can ignore it
•
Trang 34• As w is nonzero, the homogeneous vertex (x, y,
z, w)T corresponds to the 3D point (x/w, y/w,
z/w)T
• Directions (directed line segments) can be
represented with w = 0.0
Trang 35w= 1
w= 2
Homogeneous Visualization
• Divide by w to normalize (homogenize)
• W = 0? Point at infinity (direction)
(0, 0, 1) = (0, 0, 2) = …
(7, 1, 1) = (14, 2, 2) = …
Trang 36Vertex transformations
• Vertex transformations and projections can all be
represented by applying an appropriate 4 x 4 matrix to the vertex coordinates.
• All affine operations are matrix multiplications
• All matrices are stored column-major in OpenGL
• Matrices are always post-multiplied
• Product of matrix and vector is
=
•
Trang 38Programming Transformations
• Prior to rendering, view, locate, and orient:
– Eye / camera position
– 3D geometry
• Manage the matrices
– Including matrix stack
• Combine (composite) transformations
Trang 39Transformation Pipeline
• other calculations here
– material color – shade model (flat) – polygon rendering mode – polygon culling
Projection Matrix
Perspective Division
Viewport Transform
Modelview
Modelview
Projection
l l l
object eye clip normalized
device window
Trang 40– usually same as window size
– viewport aspect ratio should be same as projection
transformation or resulting image may be distorted
Trang 41Viewing Transformations
Trang 43Using gluLookAt()
• gluLookAt(eyex, eyey, eyez,
aimx, aimy, aimz,
upx, upy, upz)
– up vector determines unique orientation
– careful of degenerate positions
Trang 44the negative z-axis,
– and has the positive
y-axis as straight up
• This is the same as calling
gluLookAt(0.0, 0.0, 0.0,
0.0, 0.0, -100.0,
0.0, 1.0, 0.0);
Trang 45Using gluLookAt()
• gluLookAt(4.0, 2.0, 1.0,
2.0, 4.0, -3.0,
2.0, 2.0, -1.0);
Trang 46Projection Tutorial
Trang 47Modeling Transformations
Trang 50Scale Transformation
• glScale*(x, y, z) generates S =
•
Trang 51Rotation Transformation
• The glRotate*() command generates a
matrix for rotation about an arbitrary axis
• Rotating about the Oz axis
glRotate*(a, 0, 0, 1)
•
Trang 54Rodrigues Formula
• About (kx, ky, kz), a unit
vector on an arbitrary axis
c = cos , s = sin
•
Trang 55Bài t p ập
• Xác định ma trận của phép quay glRotatef(30.0f, 1.0f, 1.0f, 0.0f)
Trang 56Order of transformations
• In general not commutative: order matters!
Rotate then translate Translate then rotate
Trang 57Non-commutative Composition
• Scale then Translate
• Translate then Scale:
Trang 59Transformation Tutorial
Trang 60Compositing Modeling Transformations
• Problem 1: hierarchical objects
– one position depends upon a previous position
– robot arm or hand; sub-assemblies
• Solution: moving local coordinate system
– modeling transformations move coordinate system
– post-multiply column-major matrices
– OpenGL post-multiplies matrices
Trang 61/* draw sun */
glut glutWireSphere(1.0, 20, 16);
/* draw smaller planet */
glRotatef((float ) year , 0.0f, 1.0f, 0.0f);
glTranslatef(2.0f, 0.0f, 0.0f);
Trang 62Compositing Modeling Transformations
• Problem 2: objects move relative to absolute world
origin
– my object rotates around the wrong origin
• make it spin around its center or something else
• Solution: fixed coordinate system
– modeling transformations move objects around fixed
coordinate system
– pre-multiply column-major matrices
– OpenGL post-multiplies matrices
– must reverse order of operations to achieve desired effect
Trang 63Example: object spin around its center
• You’ll adjust to reading a lot of code backwards!
• Here (x, y, z) is the fixed point
– first move it to the origin (last transformation in code)
– Then rotate about the axis (ax, ay, az)
– And finally move fixed point back.
Trang 64Connection: Viewing and Modeling
Trang 65Connection: Viewing and Modeling
• Moving camera is equivalent to moving every
object in the world towards a stationary camera
• Viewing transformations are equivalent to
several modeling transformations
– View tranformation matrix E places the camera within the scene
– Then we apply E -1 to
all points in the world
– Move the eye (camera)
by updating E
Trang 66OpenGL Viewing Code
• In OpenGL, we can use the built-in transformation calls to specify the viewing transformation
• The camera is positioned in the scene with
translation by [vTx, vTy, vTz] and rotation about the X, Y, and Z axis:
Trang 67Projection Transformation
Trang 68Projection Transformation
• To lower dimensional space (here 3D -> 2D)
– Preserve straight lines
• Shape of viewing frustum
• Perspective projection
• Orthographic parallel projection
Trang 69Demo
Trang 70Orthographic projection
• The viewing volume is a rectangular parallelepiped.
• Vertexes of an object are "projected" towards infinity
• Distance from the camera doesn’t affect how large an object appears
Example: Simply
project onto xy
plane, drop z
coordinate
Trang 71Orthographic projection
• Specify the orthographic viewing frustum by
– specifying minimum and maximum x, y coordinates – Indicating range of distances along the z-axis by
specifying near and far planes
Trang 72Orthographic Projections matrix
• Here is the orthographic world-to-clip
transformation:
Trang 73Orthographic Projection in OpenGL
• This matrix is constructed with the following OpenGL
call:
glOrtho(left, right, bottom, top, zNear,zFar)
• And the 2D version (another GL utility function):
gluOrtho2D(left, right, bottom, top)
– Just a call to glOrtho() with near = -1 and far = +1
• Usually, the following code is part of the initialization
Trang 74Perspective Projections
• Artists (Donatello, Brunelleschi, and Da Vinci)
during the renaissance discovered the
importance of perspective for making images
appear realistic
• Parallel lines intersect at a point
Trang 75Perspective projection
• Characteristic of perspective projection is
foreshortening:
– The farther an object is from the camera, the smaller
it appears in the final image
Trang 76Perspective projection
• glFrustum(left, right, bottom, top,
zNear, zFar)
Trang 77Perspective projection
– fovy = vertical field of view in degrees
– aspect = image width / height at near depth
– Can only specify symmetric viewing frustums where the viewing window is centered around the –z axis.
Trang 78OpenGL Perspective Matrix
• Mapping the perspective viewing frustum in OpenGL
to clip space involves some affine transformations
• OpenGL uses a clever composition of these
transformations with the perspective projection matrix:
Trang 79Viewport Transformation
Trang 80Viewport Transformation
• The viewport is the rectangular region of the
window where the image is drawn
• Defining the Viewport
– glViewport(GLint x, GLint y, GLsizei
Trang 81Mapping the Viewing Volume to the Viewport
• The aspect ratio of a viewport should equal the aspect ratio of the viewing volume
– If the two ratios are different, the projected image will
be distorted when mapped to the viewport
Trang 82Common Transformation Usage
• 3 examples of reshape() routine
– restate projection & viewing transformations
• Usually called when window resized
Trang 83reshape(): Perspective & LookAt
public void reshape(GLAutoDrawable drawable,
int x, int y, int width, int height) { GL2 gl = drawable.getGL().getGL2();
// Set the view port (display area) to cover the entire window
gl.glViewport(0, 0, width, height);
// Setup perspective projection,
gl.glMatrixMode(GL_PROJECTION);
gl.glLoadIdentity();
// Enable the model-view transform
gl.glMatrixMode(GL_MODELVIEW);
gl.glLoadIdentity();
glu gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0,
Trang 84reshape(): Perspective & Translate
• Same effect as previous LookAt
public void reshape(GLAutoDrawable drawable,
int x, int y, int width, int height) { GL2 gl = drawable.getGL().getGL2();
Trang 85reshape(): Ortho
public void reshape(GLAutoDrawable drawable,
int x, int y, int width, int height) { GL2 gl = drawable.getGL().getGL2();
double aspect = (double) width / height;
double left = -2.5, right = 2.5;
double bottom = -2.5, top = 2.5;
gl.glViewport(0, 0, width, height);
Trang 86Additional Clipping Planes
• At least 6 more clipping planes available
• Good for cross-sections
• Modelview matrix moves clipping plane clipped
– glEnable(GL_CLIP_PLANEi)
– glClipPlane(GL_CLIP_PLANEi, Gldouble[] coeff)
Ax + By + Cz + D < 0
Trang 87Reversing Coordinate Projection
• Screen space back to world space
glGetIntegerv(GL_VIEWPORT, int viewport[4]) glGetDoublev(GL_MODELVIEW_MATRIX,