250 likes | 273 Views
Vector Spaces and Basis Transforms. Co-ordinates Bases Using a matrix to represent a basis World space, camera space, object space Building interactive objects in the virtual world. Co-ordinate spaces. What are co-ordinates?
E N D
Vector Spaces and Basis Transforms Co-ordinates Bases Using a matrix to represent a basis World space, camera space, object space Building interactive objects in the virtual world
Co-ordinate spaces • What are co-ordinates? • Well, yes... in a way. But even if they’re the same point, you might describe them using different co-ordinates. Consider the point [ x, y]: Is this the same point?
Co-ordinate spaces • Let vector P = [ x, y, z ]. • The co-ordinates of P are [ x, y, z ]. • This means that P is x units along the X axis; y units along the Y axis; and z units along the Z axis. • You could say that P is the sum of three vectors: • P = [ x, 0, 0 ] + • [ 0, y, 0 ] + • [ 0, 0, z ] • which can be broken out further: • P = [ 1, 0, 0 ] * x + • [ 0, 1, 0 ] * y + • [ 0, 0, 1 ] * z • which separates the co-ordinates (x, y, and z) from the basis vectors which give those co-ordinates meaning.
What is a basis? • Basis Vectors • If P = [ x, y, z ] then • P = [ 1, 0, 0 ] * x + // X axis • [ 0, 1, 0 ] * y + // Y axis • [ 0, 0, 1 ] * z // Z axis • These three vectors are all that’s needed to uniquely define the location of any point in the world. Any point can be described as a sum of one or more of these three vectors, multiplied by some scalar coefficient. • These three vectors, X axis, Y axis, and Z axis, form what’s called a basis, or co-ordinate system. • The particular vectors shown here-- [ 1, 0, 0 ], [ 0, 1, 0 ], and [ 0, 0, 1 ]--form what’s called the identity basis.
What is a basis? • A basis is a set of n orthogonal unit vectors, where each vector is of dimension n. • The term orthogonal means perpendicular. • A basis forms a co-ordinate system which can be used to specify the location of any point in space as a sum of one or more of the basis vectors multiplied by scalar coefficients. • Examples: • [ 1, 0, 0 ], [ 0, 1, 0 ], and [ 0, 0, 1 ] is a basis. • [ 1, 0, 0 ], [ 0, 1, 0 ], and [ 0, 0, 2 ] is not; Z is not length one. • [ 1, 0, 0 ], [ 0, 1, 0 ], and [ 0, 1, 1 ] is not; Z is not perpendicular to Y.
What is a basis? (“No-one can tell you what the basis is. You have to see it for yourself...” -Morpheus, The Basis, soon to be a major motion picture. Really.) • Why are the vectors of a basis orthogonal? Why is an n-dimensional basis made of n vectors? • Consider the following broken attempt at a basis: • X = [ 1, 0, 0 ] • Y = [ 0, 1, 0 ] • Z = [ 0, 0, 0 ] • How would you describe a point in space? • P = [ 1, 1, 0 ] : P = X + Y • P = [ 10, 20, 0 ] : P = 10 * X + 20 * Y • P = [ 0, 0, 1 ] : P = ...?!? • If all possible dimensions of space are not represented by the basis vectors, then there will be points in space which the basis vectors cannot reach.
Building a new basis • Remember the rotation matrix around the Y axis? • [ cos(45º) 0 -sin(45º) ] • R = [ 0 1 0 ] • [ sin(45º) 0 cos(45º) ] • which can be simplified, because • cos(45º) = sin(45º) = 2/2 , • as: • [ 2/2 0 -2/2 ] • R = [ 0 1 0 ] • [ 2/2 0 2/2 ] • We can multiply X, Y, and Z by R: • X’ = R * [1,0,0] = [ 2/2, 0, 2/2 ] • Y’ = R * [0,1,0] = [ 0, 1, 0 ] • Z’ = R * [0,0,1] = [ -2/2, 0, 2/2 ]
Building a new basis • Now we can build the basis B: • X = [ 2/2, 0, 2/2 ] • Y = [ 0, 1, 0 ] • Z = [ -2/2, 0, 2/2 ] …which is just like our usual identity basis except that the X and Z vectors are rotated 45 degrees in the X Z plane. • Is this a basis? • All three vectors are unit vectors • (2/2)2 + 0 2 + (2/2) 2 = 1 • All three vectors are orthogonal • X • Y = ([2/2,0,2/2]) • ([0,1,0]) = 0+0+0 = 0 • X • Z = 0 • Y • Z = 0 • There are three vectors in three dimensions... It’s a basis!
Using different bases to describe the same point in space • Consider the point P = [ 0, 0, z ]. • If we wanted to describe P in the identity basis, we would say: P = [ 1, 0, 0 ] * 0 + [ 0, 1, 0 ] * 0 + [ 0, 0, 1 ] * z ...and we could say that P is at co-ordinates (0, 0, z) in the identity basis. • We can describe the location of P in basis B: P = [ 2/2, 0, 2/2 ] * a + [ 0, 1, 0 ] * b + [ -2/2, 0, 2/2 ] * c ...for some values of a, b, and c. (a, b, and c) will be the co-ordinates of P relative to basis B.
Using different bases to describe the same point in space • Finding the co-ordinates (a,b,c) : • We know that • ( 2/2, 0, 2/2) * a + (1,0,0) * 0 + • ( 0, 1, 0) * b + = (0,1,0) * 0 + • (-2/2, 0, 2/2) * c (0,0,1) * z • Multiply through on both sides: • ( (2/2)a, 0, (2/2)a) + (1,0,0) + • ( 0, b, 0) + = (0,1,0) + • (-(2/2)c, 0, (2/2)c) (0,0,z) • Add through on both sides, x’es with x’es, y’s with y’s: • ((2/2)a-(2/2)c, b, (2/2)a+(2/2)c) = (0,0,z)
Using different bases to describe the same point in space • From ((2/2)a-(2/2)c, b, (2/2)a+(2/2)c) = (0,0,z) • we know that • (2/2)a-(2/2)c = 0 • (2/2)a+(2/2)c = z • b = 0 • and so • a-c = 0 • a+c = 2 * z / 2 • b = 0 • and thus • a = z / 2 • b = 0 • c = z / 2 • ...so the co-ordinates of P in basis B are (z/2, 0, z/2).
Using different bases to describe the same point in space • Is the point PB= (z/2, 0, z/2) in basis B really the same point in space as PI=(0,0,z) in the identity basis? • PB = (z / 2, 0, z / 2) • PI = ((2/2), 0, (2/2)) * z / 2 + • (0, 1, 0) * 0 + • (-(2/2),0, (2/2)) * z / 2 • PI = (z / 2 – z / 2, 0, z / 2 + z / 2) • PI = (0, 0, z) • Yes it is.
Using matrices to describe bases • To transform a point from one basis to another basis, we can build a matrix representation of the basis: [ [ 1 ] [ 0 ] [ 0 ] ] I = [ [ 0 ] [ 1 ] [ 0 ] ] [ [ 0 ] [ 0 ] [ 1 ] ] and [ [ (2/2) ] [ 0 ] [ (2/2) ] ] B = [ [ 0 ] [ 1 ] [ 0 ] ] [ [-(2/2) ] [ 0 ] [ (2/2) ] ] or in general, the matrix representation of a basis is the matrix whose columns are the vectors of the basis: [ [ ] [ ] [ ] ] M = [ [ X ] [ Y ] [ Z ] ] [ [ ] [ ] [ ] ]
Using matrices to describe bases • What can this representation do? • Try a simple matrix multiply: • P’ = B * PI • or rather [ [ (2/2) ] [ 0 ] [ (2/2) ] ] [ 0 ] P’ = [ [ 0 ] [ 1 ] [ 0 ] ] * [ 0 ] [ [-(2/2) ] [ 0 ] [ (2/2) ] ] [ z ] • which means P’ = [ (2/2)z, 0, (2/2)z ] • which is the co-ordinates for PB!
Using matrices to describe bases • The matrix representation of a basis B is a matrix which can transform a point from co-ordinates defined in terms of the identity basis, into co-ordinates defined in terms of basis B. • What’s more, if you construct the inverse of the matrix M, the inverse of M (written “M-1”) is a matrix which transforms points from B back into I. Which means that • PB = M * PI • PI = M-1 * PB
World Space and Camera Space • The three vectors defining a camera, • lookFrom, • lookAt • lookUp • …yield three perpendicular vectors X, Y, and Z: • Z = (lookAt – lookFrom).normalised() • Y = lookUp.normalised() • X = YZ • These three vectors define a basis BC, in which the camera looks up the Z axis towards (0, 0, 1). [ [ ] [ ] [ ] ] MC = [ [ X ] [ Y ] [ Z ] ] [ [ ] [ ] [ ] ]
World Space and Camera Space • “Camera space” vs “World space” : • Given any point PI in the virtual world, if you subtract the lookFrom of the camera from PI and then multiply the resulting vector by MC, you will have the position of P relative to the camera. • PC = MC * (PI - lookFrom) • By the same token, if you take a point in camera space—say, the point on the screen where the user just clicked (if I click at x=20, y=20 then in camera space that’s the point (20, 20, 0)) then that point can be converted to world space by • PI = (MC-1 * PC) + lookFrom • In graphics we generally refer to PI as being in world space and PC as being in camera space.
World Space and Camera Space • “Camera space” vs “Screen space” • It’s important to recall that camera space is not screen space. • Screen space is the projected form of camera space; to reach screen space a point has gone through several more transforms since being transformed into camera space. • The best way to go from world space to screen space or vice versa is to use the built-in accelerated OpenGL functions. • For further details, see gluUnProject() in the documentation.
Simplifying world and camera space • The camera-to-world equations can be rewritten as a single matrix transformation. • PC = MC * (PI - lookFrom) • PI = (MC-1 * PC) + lookFrom • The operation (PI - lookFrom) is a translation operation, shifting P by lookFrom units. Any translation can be expressed in matrix form: • [ 1 0 0 –lookFrom.x ] TlookFrom = [ 0 1 0 –lookFrom.y ] • [ 0 0 1 –lookFrom.z ] • [ 0 0 0 1 ] • Multiplying any point P by the matrix TlookFrom, above, would subtract lookFrom from P.
Simplifying world and camera space • We can extend the matrix MC, the matrix which encapsulates the transformation from world space to camera space, into a 4x4 matrix. • [ 0 ] • MC4x4 = [ MC 0 ] • [ 0 ] • [ 0 0 0 1 ] • Concatenating MC4x4 onto TlookFromcreates a matrix which will first translate any point it multiplies, then transform that point into a new basis: • WorldToCamera = MC4x4 * TlookFrom • which allows us to write • PC = WorldToCamera * PI • PI = WorldToCamera-1 * PC
World space and object space • In any hierarchical model, each object will have a transform relative to its parent. • This matrix combines both translations and rotations. • If the matrix contains only translations and rotations, then it actually contains a basis transformation and a translation. • The object’s transform is really a transform from the object’s basis to the basis of its parent. • Applying an object’s transform to (0,0,0) will translate (0,0,0) to the center of the object. Effectively, this transforms the local origin into the basis of the object’s parent, where its value is the object’s center from its parent’s point of view. • If the object has no parent, the parent basis is identity.
World space and object space • In any hierarchical model, we can refer to: • World space : the identity basis, the untransformed space. • Object space : the basis of any scene node with respect to its parent’s basis • Camera space : the basis of the camera • Basis transforms can be concatenated. • This means that just as Child’s transform willtransform points from Child space to Parentspace and Parent’s transform will carry pointsfrom Parent’s to Grandparent’s space, • P’ = MParentToGrandparent * (MChildToParent * P) • P’ = (MParentToGrandparent * MChildToParent) * P • MChildToGrandparent = MParentToGrandparent * MChildToParent Grandparent Parent Child
World space and object space • Concatenating an object’s transform onto all those of its ancestors results in a single transformation from object space to world space. • We can write this in code: M4x4 getLocalToWorld(void) { if (m_pParent) return m_pParent->getLocalToWorld() * m_transform; else return m_transform; } Vec getObjectCenterInWorld(void) { return getLocalToWorld() * Vec(0,0,0); }
World space and object space • Any basis matrix can be inverted. • For details on implementing matrix inversion, consult a linear algebra text. • The inverse of an object’s transform will be a transformation which carries values from parent space to object space. • To convert from parent space to local space, use the inverse of the local transform. • To convert from world space to local space, use the concatenation of the inverses of all the object’s ancestors. • Or, just take the inverse of the LocalToWorld transformation matrix, since that way you only invert one matrix.