SpacesConcepts
From Odwiki
Contents |
What is a space?
We use spaces all the time when working with geometry objects. When we add a transform SOP to our geometry, we are creating a space. When we translate, scale, or rotate the object that contains our SOPs, we are creating a space. And when we "parent" our object to some other object, we are creating yet another space. In short: every time we apply a transformation of some kind, we are creating a space.
We are all used to calling the initial, familiar XYZ configuration a "space", or "3D space". But there's nothing fundamentally different between it and a scaled/rotated/translated version of it. So it is equally deserving of the name "space" -- it is just a different space. Now; whether this new transformed version of the original is in any way meaningful to us depends on what we're doing, but it is just as valid a space as the original.
It should be obvious then, that there are an infinite number of possible spaces. Thankfully though, only a handful of them are actually meaningful or necessary when working with shaders.
Why should I care?
Let's pick a point in 3D space; let's choose the point {1,1,1}. Now lift your hand, and point to it with your index finger. I just did the same, and I can guarantee you that we're both pointing at different locations in space. So where is {1,1,1}? Who's right?
You chose your point relative to where you are, and I chose mine relative to where I am. But let's say that I'm 100 units away from you in the X direction. Then, from your point of view, the position I chose is actually {101,1,1}, and from my point of view, your chosen position is really {-99,1,1}. Neither of which is {1,1,1}, and yet we both claim to be pointing at it, so again, who's right?
The answer is "neither of us", "both of us", or any other combination you like -- they are all correct and incorrect. More precisely, it should be pretty obvious that the quantity {1,1,1} is utterly meaningless until we both decide what we're measuring against. If we choose your location as the reference point, then we can describe my point as {1,1,1}+{100,0,0} -- in other words, "your point plus a translation". So "my space" (the place where my point is at {1,1,1}) is a transformation of "your space", and vise-versa. Yet both of them can be considered distinct spaces.
So if we want to convey a location in space in any meaningful way, we actually need two pieces of information: its coordinates (e.g: {1,1,1}) and the frame of reference for our measurement. We call this frame of reference, a space (or a frame). And as you can see, it is quite important.
When do spaces matter?
For the purposes of shading, we only need worry about spaces when dealing with quantities that are dimensional in nature, or any quantity (regardless of dimension) whose meaning is somehow dependent on the space in which it is defined. Right away, we can include a few geometric elements which are guaranteed to be space-sensitive by definition, and require that we always think of them in conjunction with their respective spaces -- never in isolation. These are:
- Points - These represent a position in space, and have as many components as there are dimensions in that space. A 2-dimensional space will require two components [x,y] to represent a position, a 3-dimensional space, three [x,y,z], etc. We're always assuming a Cartesian representation here, where a point is just a linear combination of the basis vectors (axes) that span that space -- more on that later. Points are sensitive to all forms of transformation: translation, rotation, scaling, etc.
- Vectors - Or directional vectors, or free vectors. These represent a direction and a magnitude and, like points, will have as many components as there are dimensions. If visualized as an arrow, then the orientation of the arrow is its direction, and the length of the arrow its magnitude. Notice that we ignore where the arrow starts, or equivalently, we assume it's rooted at that space's origin (i.e: {0,0,0} in 3D). For this reason, we never consider translations when transforming them from one space to another.
- Normals - For most practical purposes, these are indistinguishable from vectors. But there are occasions when their differences must be taken into account. For the record then, normals represent a plane in 3D (and a line in 2D). This is the plane tangent to the surface at any given point -- a somewhat subtle distinction which becomes important when transforming them, as they cannot be treated in the same way as you would a point or a vector.
All of the above quantities are meaningless without a frame of reference (a space). Whenever you see one, you should train yourself to immediately think about the space in which it is defined. All calculations should be done in the same space, and this may mean you need to transform some of these quantities before using them in an expression.
Notice that all of the above items are really nothing more than a collection of numbers -- an array of numbers. There are as many numbers (elements, or components) in the array, as there are dimensions in their space. There is therefore no difference in the way each of these things needs to be stored -- i.e: points, vectors, and normals are all just arrays of numbers. As a result, VEX encapsulates them all in a single data type: they are all vectors in VEX. However, there is absolutely no relationship between the use of the word vector in VEX (which refers to the storage type for small one-dimensional arrays), and the geometric concept of a vector as described above! They may all be vectors to VEX, but one needs to take into account what they actually represent -- to the person writing a shader, a point is most definitely not the same as a vector (even though they both have the same number of components).
How are spaces represented?
Any representation that can uniquely describe every single point in space, is a valid representation of that space. In CG, however, we've grown accustomed to one representation in particular, to the point where it has likely become "second nature" to think of space in this way. It involves a set of axes (as many of these as there are dimensions in the space), where each axis is usually orthogonal (perpendicular) to all others (although it doesn't need to be), and where every point is expressed as a linear combination of these axes. This particular arrangement is known as a Cartesian Coordinate System. It is not the only way to represent space, but it's the one we know best.
If we had to encode this representation of space then, all we'd need to store would be the axes themselves (also known as the "basis vectors", or simply the "basis"). Since everything else is expressed as a combination of these, then the axes are the space. Each axis is really just a vector (a geometric vector as described above). So for 3-dimensional space we'd need three of these, and the typical set is [{1,0,0}, {0,1,0}, {0,0,1}] -- in Houdini, we call the first one "X", the second one "Y", and the third, "Z"; but this is not necessarily always the case.
So, to represent 3D space we need three vectors. Well; as it turns out, there is a very convenient mathematical construct that can represent all three axes (and more) in a single structure. It is called a Matrix. Spaces, then, are represented by matrices.
In truth, these spaces are represented as "transformation" matrices; which is to say that they are expressed relative to the implicit set of axes [{1,0,0}, {0,1,0}, {0,0,1}], with an origin at {0,0,0}. The axes of the new space are then a rotation and scaling of these, and its origin given as an offset (translation) from {0,0,0}. That root space (of which all others are a transformation) is what's commonly refered to as "world" space; it is the implicit root of all other spaces.



