 Moving into three dimensions, first we must be clear about some conventions. In a 2D coordinate system, there's only really one way to position the two perpendicular axes. Sure, you could rotate and flip them around, but that would just be viewing the same thing from a different angle. In contrast, when we add a third axis, a z-axis perpendicular to the other two, there's an arbitrary choice of which way the third axis points. Here, the z-axis points away from us when we view x and y from the normal perspective, but here the z-axis points in the opposite direction. If you take some real world pointy objects, like pencils, and try to add a third perpendicular axis to two existing perpendicular axes, you'll find that you can't just rotate one configuration around to get the other. The difference is not just a matter of perspective. To be sure, it's an arbitrary choice which system we use, but we must choose one or the other. The two choices are known as a left-hand system and a right-hand system, so cold because if you form three perpendicular axes with your thumb as x, your index finger as y, and your middle finger as z, you get the different two coordinate systems with your left-hand and right-hands. So it helps to remember the hand gesture and which digit corresponds to which axis. In the previous video in perspective, we actually used a left-hand system where x pointed right on the screen, y pointed up, and z pointed inwards away from the viewer. It's actually most common in graphics to use right-handed systems, so that's the convention we'll mostly stick with here. Now, quite confusingly, in addition to left-hand and right-hand 3D coordinate systems, we also have a so-called left-hand rule and right-hand rule for expressing the conventions of direction of rotation. The rule involves a different hand gesture, where we imagine our grip is wrapped around an axis, and our thumb points in the positive direction along the axis, and we imagine the direction of rotations for a positive angle going in the direction of our curled fingers. So let's say we go by the convention of the right-hand rule and a right-hand coordinate system. Imagine then a right-hand 3D system in which the z-axis points towards you, and imagine gripping the z-axis with your right hand. Your fingers would then be wrapping counterclockwise around the z-axis. And as you can see, this corresponds to our usual convention in 2D rotations that a positive rotation in the xy plane goes counterclockwise. So it's not surprising that in a right-hand system, the right-hand rule is the dominant convention. Getting into transformations, translations in scaling in 3D really aren't any more complicated than in 2D. The vertices simply have one more coordinate value, so performing a translation is just a matter of doing one more addition. Rotations, though, are a different matter as things get considerably more complicated in three dimensions than in two dimensions. Let's start with the simple cases of rotating around one of the three axes. It shouldn't be hard to see that rotating around one of the axes in three dimensions is really just a 2D rotation in disguise, because it will mean only affecting two of the three coordinates of a point. For example, if I rotate a point around the y-axis, only the x and z values change. So we can, in fact, use the very same 2D rotations formula as long as we're mindful of plugging in the right coordinates in place of the x and y-over formula for two-dimensional rotations. So starting with the assumption of right-hand rule rotations in a right-handed coordinate system, we rotate around the z-axis by plugging x and y into our 2D rotation formula as normal. However, for the same right-hand conventions but rotating around the x-axis, we plug our 3D y-value in place of the usual 2D x and our 3D z-value in place of our usual 2D y. This is clear if you visualize the coordinate system from the perspective where x points toward us as z conventionally does, z points up as y conventionally does, and y points right as x conventionally does. By the same logic, rotations around the y-axis in the right-hand conventions are 3D z-value plugs in for 2D x and our 3D x-value plugs in for 2D y. Now, for a left-handed system with left-hand rule rotations, the rotations around the axes actually work out the same. Rotations around the z-axis use x for x and y for y. Rotations around the x-axis use x for x and z for y. And rotations around the y-axis use z for x and x for y. But what if we mix the left-hand rule conventions with a right-hand system or mix the right-hand rule convention with a left-hand system? Well, we could do one of two things. We could either swap the two coordinate values we plug into our formula, e.g. x becomes y and y becomes x, or we could simply negate the angle of rotation. Again, though, we'll stick to the dominant convention of right-hand rule rotations in our right-hand coordinate system. So again, to be clear for us, rotations around the z-axis will use x for x and y for y. Rotations around the x-axis will use y for x and z for y and rotations around the y-axis will use z for x and x for y. Probably the most aggravating thing about rotations is that just like rotations in 2D around different pivot points are not commutative, rotations in 3D around different axes are not commutative. This can actually be quite hard to visualize sometimes, as if you just pick up a random object and try rotating it around different axes in different orders, you can easily fool yourself with some edge cases where the results of different rotation orders seem to produce similar results. So it's actually probably clear to look at some numbers. So say that I have a 3D coordinate at 900, 500, 0, which I'll rotate 30 degrees around the x-axis, 20 degrees on the y-axis, and 70 degrees around the z-axis. For all six possible orders in which I can apply these three rotations, the point ends up in a totally different position. For example, when I rotate in the order x, then y, then z, the point ends up at negative 88.4, 1023.2, 72.9, but when I rotate in the order x, then z, then y, the point ends up at negative 6.7, 993.8, 268.8. The primary use of rotations in 3D rendering is to take your object models defined in a fixed orientation around their own local coordinate system, and we want to not only position these models in our world coordinates by translation, but also orient them in any direction by rotation. So here, for example, we have an airplane model, which is represented as a bunch of vertices fixed in space, such that the nose of the plane faces straight down the x-axis, and its tail points up parallel to the y-axis. The model just as well could have been built in any other orientation, but it usually makes most sense to build models with their front facing forward and their top facing up. Though, of course, which way in our coordinate system we decide faces forward and which way faces up, that's an arbitrary decision. In this case, we decided that the x-axis points forward and that the y-axis points up. It's also most common to build models with the apparent center at the origin, because this is usually the point we want our rotations to pivot around. The question now is how do we rotate the model to any desired orientation? As we'll informally demonstrate later, we can get a model from one orientation into any other in just three, or sometimes fewer, rotations around x, y, and z. Any order of x, y, and z will work, so z than x than y, or y than z than x, can do the same work as the order x than y than z. In fact, as we'll show later, we can even achieve any orientation using just two axes, as long as we rotate around one axis than the other than the first again. For instance, x, z, x works, but x, x, z does not. In any case, when we do use all three axes, the natural question is which order of x, y, and z is easiest to use? Well, first consider the three different ways in which we usually wish to rotate an object. Once again, this is easiest to visualize with an object like a plane in one direction and a right side up. When we rotate the plane around the length of its fuselage, we're controlling its roll. When we point the nose of a plane up and down, we are controlling its pitch. And when we point the nose left and right, we're controlling its yaw. Where this gets confusing is that we can make a distinction between extrinsic and intrinsic roll pitch and yaw. While an extrinsic rotation is a rotation around the fixed axes of our coordinate system, an intrinsic rotation is relative to the current orientation of the object. So, here for example, if we again have our plane starting out facing down the x-axis, but then we rotate it, the plane then implicitly has its own axes, x-prime, y-prime, and z-prime, which are relative to its new orientation. A rotation now around the original x, y, or z would be an extrinsic rotation, but a rotation around x-prime, y-prime, or z-prime would be intrinsic. And be clear that each time we rotate the plane, we implicitly produce a new set of intrinsic rotation axes. After second rotation, we'd call the third set of axes x-prime, y-prime, and z-prime. And then after a third rotation, we'd call the fourth set of axes x-prime, y-prime, and z-prime, and so forth. A convenient fact, though one we won't prove here, is that a series of extrinsic rotations produces the same result as the reverse order of intrinsic rotations. So, for example, if we extrinsically rotate around x than y than z, we end up with the same orientation as if we instead intrinsically rotate around z than y-prime than x-prime. This is good to know, as depending upon the situation, it may be easier to think in terms of intrinsic rotations than extrinsic rotations. And so converting from a series of intrinsic rotations to a series of extrinsic rotations allows us to use our formulas for rotations around the fixed axes. Back now to the question of what is the most natural way to describe an object's orientation and space. Well, if we imagine an airplane flying somewhere in the world, we of course would first describe its position at world space, its latitude and longitude. But then for its rotation, most people would start with its heading, aka its yaw, relative to the ground. Next, most people would describe its pitch relative to its heading, aka its intrinsic pitch rotation. Lastly, we describe the plane's roll relative to its heading and pitch, aka its intrinsic roll rotation. So back to our plane model, which is oriented such that x is its roll axis, y is its yaw axis, and z is its pitch axis. In this configuration then, the natural way to orient the plane is by intrinsic rotations around y, then z-prime, then x-double-prime. Do note that in a series of intrinsic rotations, the first axis is just a normal cardinal axis, like in an extrinsic rotation, assuming that the object we're rotating starts out aligned correctly on the cardinal axis, as is the case here. In any case, to express this here as a series of extrinsic rotations, we simply reverse the order so x, then z, then y. Be clear that this all depends on the original orientation of our model. If our plane model was instead built oriented with its nose facing up the z-axis, then z would be our roll axis and x our pitch axis, so the order of intrinsic rotations would be y, then x-prime, then z-double-prime, and the extrinsic rotations would be z, then x, then y. The simplest, most general way to remember it is as the extrinsic rotations in the order roll, then pitch, then yaw. This order is significantly easier to work with than other orders of rotations. If I tell you an object is rolled 30 degrees right, then pitched 45 degrees up, and then yawed 90 degrees left, you should be able to picture the result fairly easily. And if presented with an object in some orientation, you should be able to visually estimate its roll, pitch, and yaw in the same specific order. The same cannot be said of alternative extrinsic rotation orders. Presented with an object in some arbitrary orientation, it can be difficult to visually estimate how to produce that orientation by applying rotations in some other order, like say, yaw, then roll, then pitch. We already know how to rotate objects around the coordinate axes, but what if we wish to rotate around some other line that also runs through the origin? The trick for such cases is similar to what we did to rotate in two dimensions around the right point. We'll temporarily change our frame of reference to redefine the problem into one we already know how to solve. So what we'll do here first is apply rotations which will make the line overlap any one of the three coordinate axes. It doesn't matter which. As in when we rotate to make the line overlap the x-axis, we could then perform the rotation around the line by rotating around the x-axis, something we already know how to do. Having done that, we simply then undo the rotations that changed the frame of reference. The only question now is how exactly to rotate the line onto one of the axes? Let's say we choose to rotate the line to overlap the x-axis. We have a few options, actually. We could first rotate the line to the zx plane by rotating around the z-axis, or we could do the same by rotating around the x-axis, both work. Once the line is on the zx plane, we rotate the line onto the xy plane by rotating around the y-axis. In this process, the important thing to remember is that rotating around z or x may change the line's angle to the x-axis and the zx plane, so you should figure the second rotation angle only after the first rotation, not before. We have two more options for rotating the line. We can rotate to the xy plane first by rotating around either x or y, and then we would rotate to the zx plane by rotating around z. So note that we can rotate the line to overlap the x-axis in four possible combinations of two axis rotations, either z then y, or x then y, or x then z, or y then z. As you might imagine then, if we instead decided to rotate the line to the y-axis, we could do so with four more possible combinations of two axis rotations, and likewise if we decided to rotate the line to the z-axis, we could do so with four more possible combinations of two axis rotations. Just remember that for all of these options, the first rotation may change the angle of rotation needed for the second rotation, so we should perform the first rotation in the second rotation angle. In any case, whichever axis and rotations we choose, we can then rotate around that axis to rotate around the line. Having accomplished our rotation, we put our frame of reference back by simply applying the same rotations in reverse order. So say if we rotated the line to the x-axis by rotating 20 degrees around z and then 30 degrees around y, we would undo those rotations by rotating negative 30 degrees around y and then negative 20 degrees around z. Note that we reverse both the order of the rotations and their directions. Looking now at an actual example line running through the origin, how exactly do we rotate this line to lie on the x-axis? Well, first note that a rotation around the z-axis is a rotation in the x-y plane and the slope of a vector in that plane is y of v over x of v, v here standing for vector. Likewise, rotation around the x-axis is a rotation in the y-z plane and the slope of a vector in the y-z plane is at z over its y. And lastly, rotation around the y-axis is a rotation around the z-x plane and the slope of a vector in the z-x plane is its x over its z. Notated more formally, the slope of a line in the x-y plane we would call m of x-y and it equals y of v over x of v. Likewise, m of y-z equals z of v over y of v and m of z-x equals x of v over z of v. Once we have our slopes, we can then figure the angles of those slopes by getting their arc tangents. So theta of z, the angle of the vector in the x-y plane, is arc tangent of m of x-y. Likewise, theta of x, the angle of the vector in the y-z plane is arc tangent of m of y-z and theta of y, the angle of the vector in the z-x plane is arc tangent of m of z-x. So now that we can find the angle between the vector and the coordinate planes, we can rotate the vector to line up on an axis. Again, just remember that the first rotation changes the angle we apply in the second rotation. Here's the process in more detail. If we wish to rotate theta degrees around the line, and assuming we'll use the x-axis as our temporary frame of reference, then we rotate to that frame of reference first by finding theta of z, the angle of the line in the x-y plane, then rotating around z by negative theta of z, then finding theta of y, the new angle of the line in the z-x plane, then rotating around y by negative theta of y. Now we're in a frame of reference where the line overlaps the x-axis so we can rotate around the line by rotating around the x-axis. Then we have to shift the frame of reference back, applying the same rotations from steps one and two, but reversing their order and directions. So we first rotate around y by positive theta of y and then rotate around z by positive theta of z. Then be clear that these are the same theta of z and theta of y, which we found in steps one and two respectively. So that's how we can rotate around an arbitrary axis. Again, though, using the x-axis as our temporary frame of reference is an arbitrary choice. We just as well could have used the y or z-axes. I'll also recall that for whichever axis we choose, we actually have four possible pairs of rotations around the axes that will make the line overlap the chosen axis. This gives us a total of 12 possible combinations of rotations around x, y, and z that allows us to rotate around an arbitrary axis through the origin. Not coincidentally, these are the same 12 possible combinations which allows us to rotate an object from one orientation to any other. To see why this isn't a coincidence, imagine we have an object oriented on some arbitrary line through the origin, like this airplane here. We know we can rotate the line, and thus the airplane, to point down one of the axes, and rotate the x-axis, and then we can roll the airplane around the x-axis so that it faces right side up. So no matter which way the line originally points and no matter how the airplane starts rolled around that line, we can use three rotations to get the airplane facing down the x-axis with its right side up. In other words, we know we can get the airplane from any orientation into one particular orientation using the same 12 combinations of rotations around x, y, and z which we use to rotate objects around an arbitrary line through the origin. So if we can get from any orientation to one particular orientation and three rotations, then logically we can go the other way just by reversing the moves. We can get from one particular orientation to any other orientation in three rotations. What we've stumbled upon here is a bastardized informal version of Euler's rotation theorem. The logic goes like this. Given that we can rotate an object from a given orientation to any other orientation of three or fewer rotations around the cardinal axes, and given that we can rotate around any arbitrary axis with the same combinations of three or fewer rotations around the cardinal axes, therefore, we can rotate an object from a given orientation to any other orientation by rotating around some arbitrary axis. That is, we can get to any orientation in just one rotation. Moreover, multiple rotations around different axes which all intersect at the same point can be combined into one rotation. This last point will be quite significant when we revisit rotations using matrices. For completeness's sake, here are the 12 possible combinations of three rotations around X, Y, and Z that achieve any rotation around the origin. For reasons I won't get into, the combinations are all known as Euler angles after the 18th century mathematician, but a distinction is sometimes made between the proper or classic Euler angles and Tate-Brion angles. Notice that the proper Euler angles all start and end with a rotation around the same axis. Remember much earlier when I said that we surprisingly can achieve any rotation using just rotations around two of the three cardinal axes? Well, now you should understand how this is possible. For instance, the combination X, Y, X works because, as we discussed, a rotation around X and then around Y will get any line through the origin to overlap the X axis, and a subsequent rotation around X will then effectively rotate around the line. This means that, using the combination we can rotate an object from any orientation to one particular orientation, so logically we can do the reverse with X, Y, X, rotate from one particular orientation to any other orientation. Just be very clear that while any of these combinations of rotation orders can be used to achieve any orientation, different rotation orders produce different results for the same angles. For example, if I rotate 30 degrees around X, then 50 degrees around Y, and then 20 degrees around Z, I get a different orientation of the order, first rotating 20 degrees around Z, then 50 degrees around Y, then 30 degrees around X. The same angles in a different order equal a different result. Also recall that an order of X-transit rotations is equivalent to the reverse order of intrinsic rotations, so we actually have 12 more combinations of rotations, which are the reverse order of our first 12 within intrinsic rotations. Now that we can rotate around an arbitrary axis running through the origin, what about rotations around axes not running through the origin? Well, the solution is very simple and similar to the trick we used in 2D to rotate around pivots other than the origin. Once again, we temporarily change the frame of reference, this time translating the line so that it does pass through the origin before performing a rotation and then translating back. We first select any point on the line, it doesn't matter which, we then find the vector from that point to the origin, and this vector is a translation we perform to temporarily change our frame of reference. The problem with using Euler angles as a system of describing object orientations is that Euler angles are susceptible to a phenomenon called gimbal lock. A gimbal is a device of interlocking rings with the outer ring hinged to rotate on an axis, and each inner ring hinged to rotate around the diameter of the ring immediately surrounding it. With a gimbal of three rings, the inner ring can effectively rotate in any direction from any starting orientation, except in the special case of gimbal lock. Gimbal lock occurs when any two of the rings align, and it occurs that rotations of either aligned ring produce the same rotation on the inner ring. In a sense, a degree of freedom is lost, though as Wikipedia points out the term lock is perhaps misleading because all three rings are still free to rotate. Nothing is locked in place. So how do the problems with gimbals relate to our rotations? Well, notice that a gimbal of three rings is just like a system of intrinsic Euler rotations. The rings can start out perpendicular to each other just like three axes, and a rotation of the outer ring changes the orientation of the two inner rings, and a rotation of the middle ring changes the orientation of the innermost ring. So a gimbal is actually a physical manifestation of intrinsic Euler angles. It follows then that Euler angles share the same problems. Because of scenarios like gimbal lock where two intrinsic rotation axes align, some orientations can be described in Euler angles with multiple angle values, even within a single order of rotations. So for one example, in the extrinsic rotation order X, Y, Z, the X, Y, Z angles 45, negative 90, negative 45 produce the same result as the X, Y, Z angles 0, negative 90, 0. In fact, for any set of angles where the Y angle equals negative 90 in this extrinsic rotation order, the rotations around X and Z actually have a reverse effect around the same axis. A rotation of n degrees around X equals a rotation of negative n degrees around Z. So for this rotation order X, Y, Z, any two sets of angles where the Y angle equals negative 90 and where the sum of the X and Z angles are equivalent, the rotations produced will be the same. The problem with all this redundancy is that it introduces yet more arbitrary choices for us to make on top of the choice of which of the 12 rotation orders to use. The sum effect is that Euler angles burden the users with extraneous decisions not to mention potential confusion. Another troublesome aspect of Euler angles arises in animations. We can animate a rotation frame by frame and so describe any motion we want with Euler angles. Most commonly, though, computer animations are specified by an animator using keyframes such that the object positions for frames in between get interpolated automatically. So imagine an animation system that uses Euler angles and interpolates between keyframes by simply interpolating between the respective angles. If our first keyframe has an object rotated at X, Y, Z angles 0, 30, 50 and our second keyframe has the object rotated at X, Y, Z angles 170, 40 then for each frame interpolated between those positions the X angle will smoothly change from 0 to 100 and the Y angle from the 30 to 70 and the Z angle from 50 to 40. While the resulting animation would appear smooth, the nose of the object would appear to travel in a bent arc along the sphere of rotation. So Euler angles have a tendency to frustrate animators because they often produce unintuitive animations. The purpose for this is to have the animation system do a more sophisticated form of interpolation such that the rate of change between angles differs for each coordinate. The other fix is to use an alternative system of describing rotations called quaternions. Unfortunately quaternions are well beyond our scope here as they involve complex numbers in a way that goes beyond high school algebra. They were first described by a 19th century iris mathematician William Rowan Hamilton who famously in a moment of inspiration devised the key formula that equals j squared equals k squared equals ijk equals negative 1 while taking a walk and then immediately carved the formula into a stone of the bridge he stood on. While a bit tricky to grasp, quaternions don't suffer the same drawbacks as Euler angles and so are commonly used in 3D animation. I'd like to cover them in a later video but I'm not well qualified to do so as I barely understand them myself so I can't make any promises. Perhaps I'll later just direct you to other resources that cover quaternions well.