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 4Camera Analogy and Transformations
• Viewing transformations
– tripod–define position and
orientation of the viewing
volume in the world
Trang 5Coordinate 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)
5
Trang 6Affine Transformations
• Want transformations which preserve geometry
– lines, polygons, quadrics
• Affine = line preserving
– Rotation, translation, scaling
– Projection
– Concatenation (composition)
Trang 7Homogeneous Coordinates
• Each vertex is a column vector
• If a is nonzero, then (x, y, z, w) T and (ax, ay, az, aw) T
represent the same homogeneous vertex
• A 3D Euclidean space point (x, y, z) T becomes the
homogeneous vertex (x, y, z, 1.0) T
• 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
•
7
Trang 8Vertex transformations
• Vertex transformations (rotations, translations,
scaling, and shearing) and projections (such as
perspective and orthographic) 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 9• Specify operation (glRotate, glOrtho)
– Programmer does not have to remember the exact
matrices
9
Trang 10Programming Transformations
• Prior to rendering, view, locate, and orient:
– Eye / camera position
– 3D geometry
• Manage the matrices
– Including matrix stack
• Combine (composite) transformations
Trang 11Transformation Pipeline
• other calculations here
– material color – shade model (flat) – polygon rendering mode – polygon culling
Modelview
Modelview
Projection
l l l
object eye clip normalized device window
Trang 12– usually same as window size
– viewport aspect ratio should be same as projection
transformation or resulting image may be distorted
– glViewport(x, y, width, height)
Trang 13Viewing Transformations
Trang 14Viewing Transformations
• Position the camera/eye in the scene
– place the tripod down; aim camera
• To "fly through" a scene
– change viewing transformation and
redraw scene
• gluLookAt(eyex, eyey, eyez,
aimx, aimy, aimz,
upx, upy, upz)
– up vector determines unique orientation
– careful of degenerate positions
tripod
Trang 15Using gluLookAt()
• In the default position, the camera is at the origin, is
looking down the 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 16Default View
• In the default position,
– the camera is at the
Trang 17Using gluLookAt()
• gluLookAt(4.0, 2.0, 1.0, 2.0, 4.0,
-3.0, 2.0, 2.0, -1.0);
Trang 18Projection Tutorial
Trang 19Modeling Transformations
Trang 21Translation Transformation
• The call glTranslate*(x, y, z) generates T, where
T =
•
Trang 22• glScale*(x, y, z) generates S =
•
Scale Transformation
Trang 23Rotation Transformation
• The glRotate*() command generates a matrix for
rotation about an arbitrary axis
• Rotating about the Oz
axis glRotate*(a, 0, 0, 1)
•
Trang 25General rotations
• The call glRotate*(a, x, y, z) generates R as follows:
– Let v = (x, y, z)T, and u = v/||v|| = (x’, y’, z’) T.
– Also let S =
– and M = uu T + (cos a) (I − uu T ) + (sin a) S
– Then R =
•
Trang 26Bài tập
• Xác định ma trận của phép quay glRotatef(30.0f,
1.0f, 1.0f, 0.0f)
Trang 27Order of transformations
• In general not commutative: order matters!
Rotate then translate
Trang 28Transformation Tutorial
Trang 29Compositing Modeling Transformations
• Problem 1: hierarchical objects
– one position depends upon a previous position
– robot arm or hand; sub-assemblies
• Solution 1: moving local coordinate system
– modeling transformations move coordinate system
– post-multiply column-major matrices
– OpenGL post-multiplies matrices
29
Trang 30Compositing 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 2: 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 32Connection: Viewing and Modeling
Trang 33Connection: Viewing and Modeling
• Moving camera is equivalent to moving every object in
the world towards a stationary camera
• Viewing transformations are equivalent to several
Trang 34OpenGL 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:
void setupView(void) { // Inverse viewing transformation glRotatef( -vAngleZ, 0.0, 0.0, 1.0 );
glRotatef( -vAngleY, 0.0, 1.0, 0.0 );
glRotatef( -vAngleX, 1.0, 0.0, 0.0 );
glTranslatef( -vTx, -vTy, -vTz );
void setupView(void) { // Inverse viewing transformation glRotatef( -vAngleZ, 0.0, 0.0, 1.0 );
glRotatef( -vAngleY, 0.0, 1.0, 0.0 );
glRotatef( -vAngleX, 1.0, 0.0, 0.0 );
glTranslatef( -vTx, -vTy, -vTz );
Trang 35Constructing a Frame
• The cross product between the up and the look-at
vector will get a vector that points to the right.
r = up x a
• Finally, using the vector a and the vector r we can synthesize a new vector u in the up direction:
Trang 36• Rotation takes the unit world to our desired camera:
Trang 37• Translation to the eye point:
Trang 38Composing the Result
• The final camera transformation is:
Trang 39The Viewing Transformation
• Transforming all points P in the world with E -1 :
• Where these are normalized vectors
a = P eye – P aim
r = up x a
u = a x r
Trang 40Projection Transformation
Trang 41Projection Transformation
• To lower dimensional space (here 3D -> 2D)
– Preserve straight lines
• Shape of viewing frustum
• Perspective projection
• Orthographic parallel projection
41
Trang 42Demo
Trang 43Orthographic 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
Trang 44Orthographic 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 45Orthographic Projections matrix
• Here is the orthographic world-to-clip
transformation:
Trang 46Orthographic 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 47Perspective 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 48Perspective projection
• Characteristic of perspective projection is
foreshortening:
– The farther an object is from the camera, the smaller it
appears in the final image
Trang 49Perspective projection
• glFrustum(left, right, bottom, top,
zNear, zFar)
49
Trang 50Perspective projection
– fov = 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 51OpenGL 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 52Viewport Transformation
Trang 53Viewport Transformation
• The viewport is the rectangular region of the
window where the image is drawn
• Defining the Viewport
– glViewport(GLint x, GLint y, GLsizei
width, GLsizei height)
Trang 54Mapping 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 55Common Transformation Usage
• 3 examples of resize() routine
– restate projection & viewing transformations
• Usually called when window resized
55
Trang 56resize(): Perspective & LookAt
void resize( int w, int h )
Trang 57resize(): Perspective & Translate
• Same effect as previous LookAt
Trang 58GLdouble bottom = -2.5, top = 2.5;
glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION);
Trang 59Additional Clipping Planes
• At least 6 more clipping planes available
• Good for cross-sections
• Modelview matrix moves clipping plane
Trang 60Reversing Coordinate Projection
• Screen space back to world space
glGetIntegerv(GL_VIEWPORT, GLint viewport[4]) glGetDoublev(GL_MODELVIEW_MATRIX,
GLdouble *objx, *objy, *objz)
• gluProject goes from world to screen space