![]() ![]() Something that doesn’t move will be at the center of the world. You apply this matrix to all your vertices at each frame (in GLSL, not in C++!) and everything moves. Easy, you just learnt do do so : translation*rotation*scale, and done. We’d like to be able to move this model, maybe because the player controls it with the keyboard and the mouse. The X,Y,Z coordinates of these vertices are defined relative to the object’s center : that is, if a vertex is at (0,0,0), it is at the center of the object. This model, just as our beloved red triangle, is defined by a set of vertices. This is the way everybody does, because it’s easier this way. You may not use this (after all, that’s what we did in tutorials 1 and 2). The Model, View and Projection matrices are a handy tool to separate transformations cleanly. Mat4 transform = mat2 * mat1 vec4 out_vec = transform * in_vec The Model, View and Projection matricesįor the rest of this tutorial, we will suppose that we know how to draw Blender’s favourite 3d model : the monkey Suzanne. For now, we’ll simply ask the computer to do it : Matrix-matrix multiplication is very similar to matrix-vector multiplication, so I’ll once again skip some details and redirect you the the Matrices and Quaternions FAQ if needed. It’s still the same size, and at the right distance. You get a big ship, centered on the origin. Every coordinate is multiplied by 2 relative to the origin, which is far away… So you end up with a big ship, but centered at 2*10 = 20. Its center is now at 10 units of the origin. For instance, given a ship model (rotations have been removed for simplification) : Make one step ahead ( beware of your computer ) and turn left Īs a matter of fact, the order above is what you will usually need for game characters and other items : Scale it first if needed then set its direction, then translate it. Writing the operations in another order wouldn’t produce the same result. !!! BEWARE !!! This lines actually performs the scaling FIRST, and THEN the rotation, and THEN the translation. TransformedVector = TranslationMatrix * RotationMatrix * ScaleMatrix * OriginalVector … ie our original (0,0,-1,0) direction, which is great because as I said ealier, moving a direction does not make sense. Let’s now see what happens to a vector that represents a direction towards the -z axis : (0,0,-1,0) So our transformation didn’t change the fact that we were dealing with a position, which is good. … and we get a (20,10,10,1) homogeneous vector ! Remember, the 1 means that it is a position, not a direction. So if we want to translate the vector (10,10,10,1) of 10 units in the X direction, we get : Where X,Y,Z are the values that you want to add to your position. These are the most simple tranformation matrices to understand. ( have you cut’n pasted this in your code ? go on, try it) Translation matrices Vec4 transformedVector = myMatrix * myVector // Yeah, it's pretty much the same than GLM Mat4 myMatrix vec4 myVector // fill myMatrix and myVector somehow Now this is quite boring to compute, an we will do this often, so let’s ask the computer to do it instead. You’ve got your new x ! Do the same for each line, and you’ll get your new (x,y,z,w) vector. Move your left finger to the next number (b), and your right finger to the next number (y). Put your left finger on the a, and your right finger on the x. Matrix x Vertex (in this order !!) = TransformedVertex This is done by multiplying the vertex with the matrix : They will allow us to transform our (x,y,z,w) vertices. In 3D graphics we will mostly use 4x4 matrices. For instance, a 2x3 matrix can look like this : Simply put, a matrix is an array of numbers with a predefined number of rows and colums. Transformation matrices An introduction to matrices Homogeneous coordinates allow us to use a single mathematical formula to deal with these two cases. What could mean “translate a direction” ? Not much. However, for a translation (when you move the point in a certain direction), things are different. When you rotate a point or a direction, you get the same result. ![]() What difference does this make ? Well, for a rotation, it doesn’t change anything. If w = 0, then the vector (x,y,z,0) is a direction.If w = 1, then the vector (x,y,z,1) is a position in space.This will be more clear soon, but for now, just remember this : Until then, we only considered 3D vertices as a (x,y,z) triplet. This is the single most important tutorial of the whole set. The ship stays where it is and the engines move the universe around it. Cumulating transformations : the ModelViewProjection matrix.The Model, View and Projection matrices.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |