4D vectors and W coordinate

Started by
5 comments, last by someusername 18 years, 4 months ago
I know what a 3D vector is, and the X, Y, Z coordinates, but what is the W coordinate? And what would you use the W coordinate or 4D vector for in a game?
Advertisement
Quick answer: the term to google for is 'homogeneous coordinates' - that should give you plenty of info. In practical terms, the w component is most commonly used for projection. If you're not doing the projection calculations yourself (which is probably the case if you're using an API such as OpenGL or D3D), you usually don't need to worry about the w component.
Quaternions need 'm. So do 4x4 matrices. They also come in handy when you want to do the same operation 4 times in a row (with current SIMD extensions/co-processors).
Hi all...
well, there are quite a few reasons for that as far as I understand...
This component is often called the 'homogenus' coordinate, but I can't tell for sure if they mean the strict mathematical sense of 'homogenous coordinates'... anyway... Haven't really gotten down to check it...

First, 3d math in games works with 4x4 matrices, not 3x3 as one would expect for three dimensional spaces. This is because you can't perform translation to all 3d vectors by multiplying them with a 3x3 matrix. So the matrix has to be 4x4 and this means that vector must have 4 components for the multiplication to be valid.
Furthermore, 4x4 matrices align more efficiently to memory indices, thus allowing for better performance.

Others also use the the w coordinate as a flag for whether a vector represents a point in 3d space or merely a direction. (W component:0->direction, 1->point)
With that in mind, adding a 'point' and a 'direction' would yield a new point, or subtracting a 'point' from another 'point' yields the 'direction' from one point to the other, which is the reasonable thing...

What else... The most important! If you properly set up the projection matrix in your 3d game, then the w-component (after the transformation) actually turns out to be the exact distance of the transformed point to the camera, which enables you to do a lot of things. Depth testing i.e., (I'm sured you've heard of W-depth buffers), fog effects, camera "depth-of-field" blur and a bunch of cool stuff, without any extra cpu overhead. I mean... you had to project the point anyway...

That's all I can think of for now, sure there are many more... This W thing is cool!
Ah I see, just another question, what are matrices used for? I know they're used for manipulations, but in what way. And what do you apply them to? You're own math, or the API (I'm using OpenGL)? Also what are projection matrices I keep hearing about? Is the matrix just a way to hold all the information? Like an array or something? And how does OpenGL use matrices?
Quote:Ah I see, just another question, what are matrices used for? I know they're used for manipulations, but in what way. And what do you apply them to? You're own math, or the API (I'm using OpenGL)? Also what are projection matrices I keep hearing about? Is the matrix just a way to hold all the information? Like an array or something? And how does OpenGL use matrices?
The answers to all your questions are out there :) I'd start with some of the articles here on gdnet. Then maybe try the book '3D Math Primer' or another introductory reference. The OpenGL Red Book might also help. And then there's google. Be aware that not all internet articles and references are completely reliable, but by looking at many such references you can 'put the pieces together' and develop an accurate understanding of the material.
Matrices are mathematical objects, just like scalar numbers and vectors. They have their own properties and calculus and they are widely used because they provide very useful features but they also allow for very compact representation of data.

In short, a matrix is a rectangular representation of data in a specific number of rows and columns. A matrix multiplied by a matrix yields another matrix. A vector multiplied by a matrix yields another vector, and this is how they are used for manipulating 3d geometry. Their most powerful feature however, is that the effects performed by a single matrix are passed to its product when you multiply it with an another matrix. The product matrix would then cary the effects of both previous matrices. If there were no matrices, in a 3d game you would have to multiply all your objects' vertices with sines/cosines of angles to rotate them in X, then in Y, then Z, then scale them i.e., then add some constant to translate the object somewhere in your scene... Then rotate them in a proper way to simulate them being viewed from a camera somewhere... This would take "days" for a simple frame of 10000 triangles, because you'd have to multiply the same points over and over to get the effect you want.

What programmers actually do, is prepare a matrix for the rotation of each axis, multiply them together to get a combined rotation matrix, then multiply that with a scale matrix, then further multiply with a translation matrix, this matrix is also multiplied with the infamous 'view-projection' matrices and you get a new 4X4 matrix (16 numbers) that contains the entire transformation for your object, based on how you created it step-by-step. This isn't 'CPU expensive' because you're actually doing 16 dot products per multiplication, which are implemented really fast with SSE/3DNow. (aah, another reason for 4D vectors!) If you multiply the object's vertices (vectors) by that matrix the point will end up exactly where you wanted, as being viewed from the point you wanted, with the camera properties you selected. This way you get to save a ton of multiplications... all the effects of the matrices are passed into their product, multiply once your points with it, and you're ready.

The view matrix is a matrix that essentially applies a kinda 'reverse' rotation to objects, in order to simulate them as being viewed from a specific point in your scene. For example if your camera was a little to the right of an object, it would rotate it a little to the left, to simulate your point of view.

The projection matrix generally applies perspective to the scene. It's the last transformation in the product matrix (careful, matrix multiprication isn;t commutative -like scalars- so the sequence is extremely important) and it simply makes things closer to the camera look bigger and elongated, rather than those away.

Most gfx APIs provide extensive mathematical interfaces for matrices, I can't help you with openGL, you'll have to look into it yourself. I suggest you look for papers about 3d transformations in games and openGL documentation, because there are lots of aspects (and pitfalls) in this.
Good luck

This topic is closed to new replies.

Advertisement