550 likes | 563 Views
Interactive Computer Graphics Transformations. James Gain and Edwin Blake Department of Computer Science University of Cape Town July 2002 jgain@cs.uct.ac.za. Map of the Lecture. Vector Geometry: Vector, Affine and Euclidean Spaces Coordinate Systems:
E N D
Interactive Computer GraphicsTransformations James Gain and Edwin Blake Department of Computer ScienceUniversity of Cape Town July 2002jgain@cs.uct.ac.za Collaborative Visual Computing Laboratory
Map of the Lecture • Vector Geometry: • Vector, Affine and Euclidean Spaces • Coordinate Systems: • Euclidean, Polar, Homogenous Coordinate Systems • Transformations: • Coordinate System, Object, Viewing Transformations Transformations Viewing Shading Interactive Computer Graphics Contents
Vector Geometry • Enables fundamental operations with points and vectors • Independent of any particular co-ordinate system • Robust, simple and efficient algorithms • Operations: • Closest point, inside/outside and intersection tests • Entities: • Vectors, points, lines, planes and polygons Interactive Computer Graphics Contents
Points • Discrete positions • Represented by co-ordinate triples relative to co-ordinate axes • Class Point { double x,y,z; }; • But addition, subtraction, multiplication and division of points are not defined Interactive Computer Graphics Contents
Illegal Operation on Points • The results of arithmetic operations on points: • depend on the co-ordinate system • have no geometric meaning • A contradiction: finding the midpoint is legal. Interactive Computer Graphics Contents
Vectors • Pointers indicating direction and magnitude • Also represented by co-ordinate triples but with no fixed position • Class Vector { double i,j,k; }; • Clearly distinct from points Interactive Computer Graphics Contents
Vector Space • Domain in which vectors live • Consists of: • A set of vectors • Two closed operations: addition and scalar multiplication • Special zero vector, , for which and • Can be of any dimension, . Typically 2D or 3D • Conceptually similar to an object-oriented class (data = vector, methods = operations) Interactive Computer Graphics Contents
Implementing a Vector Space • class Vector • { double i,j,k; • inline void scale(double c) • { i = i * c; j = j * c; k = k * c; • } • inline Vector add(Vector a, Vector b) • { i = a.i + b.i; • j = a.j + b.j; • k = a.k + b.k; • } • }; Interactive Computer Graphics Contents
Affine Space • Domain in which points live • Consists of: • A set of points • An associated vector space • Two extra operations: (a) subtraction of two points to form a vector (b) addition of a point and vector to produce a new point • Unlike the zero vector there is no distinguished point Interactive Computer Graphics Contents
Implementing an Affine Space • class Point • { double x,y,z; • inline void sub(Point q, Vector * v) • { v->i = q.x - p.x; v->j = q.y - p.y; • v->k = q.z - p.z; • } • inline void plus(Point p, Vector v) • { x = p.x + v.i; y = p.y + v.j; • z = p.z + v.k; • } • }; Interactive Computer Graphics Contents
Addition and Multiplication of Points • undefined • vector • BUT midpoint calculation point • Recast in terms of legal operations: • point • point iff Interactive Computer Graphics Contents
Euclidean Space • An affine space with the additional concept of distance • Consists of: • An affine space • Two new operations, dot product and cross product • Distance between two points = length of the vector between them = Interactive Computer Graphics Contents
Length of a Vector • From Pythagoras the length of a vector is: • In order to normalize a vector it is scaled by the reciprocal of its length: Interactive Computer Graphics Contents
Dot Product • Variously known as inner, dot, or scalar product. • Implementation: • double dot(Vector v, Vector w) { return (a.i * b.i + a.j * b.j + a.k * b.k); • } • where is the angle between and fitted tail to tail. Interactive Computer Graphics Contents
Cross Product • Variously known as the outer, cross or vector product. • and • Implementation: • Vector::cross(Vector a, Vector b) { i = (a.j * b.k) - (a.k * b.j); j = (a.k * b.i) - (a.i * b.k); k = (a.i * b.j) - (a.j * b.i); • } • Produces a vector orthogonal to the arguments in the same ‘sense’ as the co-ordinate axes. Interactive Computer Graphics Contents
Lines • From two points, and , a spanning line segment or interpolating line can be found. • Geometric entities usually have two forms: • Implicit • Parametric • Line equations (a point on the line satisfies) • Implicit • Parametric ( parameter) Interactive Computer Graphics Contents
Planes • From three points, , and , an interpolating plane can be found. • Plane equations (a point on the plane satisfies) • Implicit ( normal, point on the plane) • Parametric ( parameters) • The implicit equation can be rewritten in the more familiar form: where Interactive Computer Graphics Contents
Derivation of the Implicit Plane • Given the three points, , and . • Find a vector normal to the plane • This assumes that the points are not collinear (all in a straight line). • Set Interactive Computer Graphics Contents
Exercise: Backface Culling • Given: • A left-handed 3D co-ordinate system • A triangle with vertices, , and , which form a clockwise ordering when viewed from the front • A viewpoint • Design an algorithm which determined whether the back or front of is visible from Interactive Computer Graphics Contents
Solution: Backface Culling • IF THEN RETURN ‘undefined’ • IF THEN RETURN ‘side-on’ • IF THEN RETURN ‘back-facing’ • ELSE RETURN ‘front-facing’ Interactive Computer Graphics Contents
Cartesian Co-ordinate Systems • Consists of a reference point (the origin) and three mutually perpendicular lines passing through this point (the axes) labelled • A labelling of the axes has either a left-handed or right-handed orientation • Right-hand rule: a co-ordinate system is right-handed if, whenever the thumb is aligned with +ve and the forefinger with +ve , then the index finger points in the direction of +ve . Otherwise the orientation is left-handed • If unit vectors are aligned with the different axes then together they form an orthonormal (orthogonal and normal) basis for the co-ordinate system Interactive Computer Graphics Contents
Polar Co-ordinate Systems • Alternative system using rotation angles • Orient a ray by rotating in the plane ( ) and then elevating it towards the -axis ( ). A point is then located at a distance ( ) along the ray • A point is defined by the polar co-ordinates • Conversion from polar to Cartesian co-ordinates: Interactive Computer Graphics Contents
Transformations • Objects typically consist of a collection of interrelated points, e.g. a polygon-mesh • A predefined object can be modified to an arbitrary location, orientation and size by applying the same transformation to all its constituent points • BUT, this only work if the transformation is affine • Preserve straight lines • Need only transform the endpoints of a line rather than every single point along it • Transformations are extremely useful in both modelling and rendering Interactive Computer Graphics Contents
Basic 2D Transformations • Scale • About the origin. • By factors and . • If then balanced, otherwise unbalanced. • Translate • Along the vector . • Reflect • In one or both of the co-ordinate axes. Interactive Computer Graphics Contents
Further 2D Transformations • Shear • Parallel to an axis. • By an angle . • Rotate • About the origin. • Counter-clockwise by an angle . Interactive Computer Graphics Contents
Derivation of Rotation Interactive Computer Graphics Contents
The Matrix Representation of 2D Transformations • A transformation can be encoded by pre-multiplying a transformation matrix, with a column vector of co-ordinates, : • Be aware that some texts post-multiply a co-ordinate row vector with a transformation matrix: • The transformation matrices are transposed with respect to each other. Interactive Computer Graphics Contents
Transformation Matrices • Scale (by factors and ) Shear (parallel to by ) • Reflect (in ) Rotate (by angle ) Interactive Computer Graphics Contents
Homogenous Co-ordinates • 2D Translations cannot be encoded using matrix multiplication because points at the origin never move. • Instead use homogenous co-ordinates • These are projections of 3D points along a ray from the origin onto the plane Interactive Computer Graphics Contents
Homogenous Projections • An infinite number of homogenous co-ordinates map to every 2D point. are all equivalent to . • Points at infinity have and cannot be projected onto the plane because this would involve division by zero • These points can be used to represent vectors • Conversion: Interactive Computer Graphics Contents
Matrices for Homogenous Co-ordinates • where is a standard transformation • Translation: Interactive Computer Graphics Contents
Concatenating Transformations • It is frequently necessary to perform a sequence of transformations on the same object. • These can be merged into a single transformation by matrix multiplication. • Example: shearing then reflecting Interactive Computer Graphics Contents
Order is Important • Matrix multiplication is not commutative • The order in which transformations are combined affects the outcome. Interactive Computer Graphics Contents
Non-Standard Transformations • Centred at a point other than the origin • Or involving a line which is not parallel to any co-ordinate axis. • Example: A rotation by about the point • (1) Translate to the origin, • (2) Rotate by about the origin, • (3) Translate the origin back to , • The combined transformation is Interactive Computer Graphics Contents
Matrices for Off-Origin Rotation Interactive Computer Graphics Contents
Exercise: Composite Reflection • Write down but do not multiply out the composite transformation that reflects a point in an arbitrary line passing through point and , where • Hint: you will need to use translation, rotation and reflection Interactive Computer Graphics Contents
Solution: Composite Reflection Translate to the origin, Rotate the vector onto the -axis, , by the angle between and Reflect in the -axis, Reverse the rotation, Reverse the translation, Interactive Computer Graphics Contents
Composite Reflection Matrices Interactive Computer Graphics Contents
3D Transformations • Employ 3D homogenous co-ordinates • Transformation matrices: • translation scaling reflection Interactive Computer Graphics Contents
3D Rotations • Rotation: (counterclockwise around the positive axis) • about about about Interactive Computer Graphics Contents
Transformation Pipeline Modelling Transform Object in object co-ordinates Object in world co-ordinates Viewing Transform Object in 2D screen co-ordinates Perspective Projection Object in viewing co-ordinates Interactive Computer Graphics Contents
Modelling Transforms • Transformation of an object from a local co-ordinate frame to the world co-ordinate frame. • The local system ( ) is expressed relative to the world system ( ) as three vectors , and at an origin • From to : Interactive Computer Graphics Contents
Viewing Transforms • Need to transform an arbitrary co-ordinate system to the default viewing co-ordinate system (with the eye at the origin and the screen perpendicular to ). • The camera is specified in world co-ordinates as: • A camera position at . • A look point (screen centre) at . • An up vector (which orients the camera). • Transformations: • Translate the camera to the origin. • Scale by so that the camera to screen distance is . • Align the vector through rotation with the axis. • Rotate around so that is aligned with the axis. Interactive Computer Graphics Contents
Rotation about an Arbitrary Vector • Task: • Rotate around the vector from to by an angle • Solution: • Translate to the origin • Rotate around by so that is aligned with the plane • Rotate around by to align with the axis • Rotate around by • Reverse rotations 2,3 • Reverse translation 1 Interactive Computer Graphics Contents
OpenGL Transformations • Current Transformation Matrix: • The transformation state of the system • A concatenated matrix applied to all subsequent vertices • Transformations in OpenGL: • In OpenGL CTM is a product of the Projection (GL_PROJECTION) and Model-View (GL_MODELVIEW) matrices • Functions to change matrix state, set and concatenate matrices • Functions to rotate, translate and scale Interactive Computer Graphics Contents
Example: OpenGL Transformations • Task: halve the x-length of an object centred at and rotate it by about the vector • glMatrixMode(GL_MODELVIEW); • glLoadIdentity(); • glTranslatef(4.0, 5.0, 6.0); • glRotatef(45.0, 1.0, 2.0, 3.0); • glScalef(0.5, 1.0, 1.0); • glTranslatef(-4.0, -5.0, -6.0); • Access the Model View matrix • Load the identity transformation • OpenGL tansformations added in reverse order: • Translate the origin to • Rotate by about • Scale by • Translate to the origin Interactive Computer Graphics Contents
Controlling OpenGL Matrices • Order of Transformations: • The transformation specified most recently is the one applied first • Conceptually analogous to pushing matrices onto a stack – matrices are applied in the reverse of their specification • Matrices can be loaded or concatenated directly: • glLoadMatrix(tflat) set current state to T • glMultMatrix(tflat) postmult T with current state • Glfloat tflat[16] is an expansion of T[4][4] arranged in columns ( i.e tflat[2] = tmat[0][2] ) • The state of a matrix can be saved: • glPushMatrix() saves the current matrix • glPopMatrix() restores the last saved state Interactive Computer Graphics Contents
Euler Angles (Azimuth, Elevation, Roll) • Historically popular but flawedparametrizationof orientation • A general rotation is constructedas a sequence of rotations about3 mutually orthogonal axes:rolls about , and • The order of the rolls is significant • Arbitrarily choose the ordering A roll about by , followed with a roll about by , and finally a roll about by • Euler angle interpolation is not very stable. Especially with a small rotation in one axis and large rotation in another Interactive Computer Graphics Contents
Gimbal Lock • A gimbal mechanism consists of three concentric rings on pivots which support a compass or gyroscope. • Gimbal lock occurs when two of the rings are accidentally aligned. • Euler angles are also susceptible. If one axis is rolled into alignment with another then a degree of rotation freedom is lost. • Unlike translations, rotations relative to separate axes are not independent. • Example: a -roll of rotates the -axis onto the-axis so that rolls about and are indistinguishable. Interactive Computer Graphics Contents
Lerping Euler Angles • Two Euler rotations and can be linearly interpolated to obtain inbetween rotations: • Problems: • This does not produce a ‘natural’ steady rotation about a single vector but may instead cause weird oscillations. Reason: the rolls about are not independent. • The resulting interpolation differs depending on the ordering of the Euler angles. Reason: the rolls about are not commutative. Interactive Computer Graphics Contents