Transform states, matrices...I don't really know what they are!

Started by
8 comments, last by Normie 22 years ago
I just suffered from the profound realization that I actually have no idea what transform states are, the differences between them (world, view, etc), how matrices relate, the difference between transformed and non-transformed vertices, and a whole bunch of other basic questions. Can anyone point me to a site that actually explains what all these things are? :/ -Normie
I am a devout follower of the"Lazy Programmer's Doctrime"(tm)...and I'm damned proud of it, too!-----"I came, I saw, I started makinggames." ... If you'll excuseme, I must resume my searchfor my long lost lobotomy stitches.
Advertisement
I''m not sure about sites, but I point people to the OpenGL "red book" (yes, even for DX).

Also, you may want to look into books like those by Foley and Van Dam and others. That''s the best place for the basics.

Just curious - have you had matrices in math classes?
Author, "Real Time Rendering Tricks and Techniques in DirectX", "Focus on Curves and Surfaces", A third book on advanced lighting and materials
If you do read the openGL redbook, keep in mind that openGL has one concatenated MODELVIEW matrix, whereas Direct3D maintains a separate view and world (model) matrix.

To sum it up real fast, the world / model matrix represents an objects position / orientation in world space. The view matrix represents the camera position / orientation in world space. The projection matrix determines how 3D world coordinates get projected onto your 2D monitor. If you use hardware transformed vertices, it means you give everything in world coordinates, and supply the 3 matrices. The card multiplies them all together determines the projected vertex coordinates (among other things like lighting, clipping, and texturing) and draws the polygons on the screen.

See the matrix and quaternion FAQ for more on matrices:
http://skal.planet-d.net/demo/matrixfaq.htm
I should probably clarify a little...I know about matrices, I know how to matrix multiply by hand (ick), etc. I know about vectors, cross products, dot products, etc. I know about all of the math elements on their own, I just don't know how matrices relate to 3D, or the difference between pretransformed/untransformed vertices, for that matter. (EDIT)I'm still kinda confused on which transform state to use (world, view, etc.), and when, too. (END EDIT)

Oh, by the way, here's a question someone else wants me to ask (I'm quoting here): "Besides matrix transforms, is there any way to transform a primitive without locking the vertex buffer?" (I'm serious, by the way; this isn't one of those "I have this friend..." things, he just doesn't wanna bother create an account. )

-Normie

PS. If I sound confused, it's because I am.

[edited by - normie on March 18, 2002 4:39:58 PM]
I am a devout follower of the"Lazy Programmer's Doctrime"(tm)...and I'm damned proud of it, too!-----"I came, I saw, I started makinggames." ... If you'll excuseme, I must resume my searchfor my long lost lobotomy stitches.
Well, technically, the three different matrices (world, view and projection) can be used for any transformations you want.
The transformations will always be applied in the World, then View, then Project order though.
Usually, you would use the transformations in the standard/official way though. The World matrix is used to move an object to the correct position in the world. The View matrix is used to move everything in the world so that the camera is in the right place (instead of moving the camera to a position, you move the world in the opposite way - it ends up looking the same but it makes projection easier). The Projection matrix is then used to flatten everything into 2D (the screen), while applying perspective.

If it makes it any easier to understand, internally, what (I think) is going on is:
Each vertex is multiplied by the world matrix. The resulting vertex (which is in world space) is then multiplied by the view matrix. The the result of that (which is in view space or camera space) is multiplied by the projection matrix. And finally the thing is rasterised (drawn onto the screen - which is a raster device).

The difference between un-transformed (without RHW) and transformed (with RHW) vertices is very simple. Un-transformed vertices are still in object space (they have not been multiplied by any matrices yet). Transformed vertices are in screen space - they are ready to be rasterised.
You would only put transformed vertices in a vertex buffer "manually" if you were doing work in 2D (or if you had a very special and strange reason - or I suppose if you were insane )

And for you friend, AFAIK you cannot move a vertex without either using matrices or locking the vertex buffer.

I hope some of that stuff helps,

John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
Thanks for the info... I guess I''ll just have to figure this stuff out. :/

-Normie

PS. My friend "thanks" you for that info.
I am a devout follower of the"Lazy Programmer's Doctrime"(tm)...and I'm damned proud of it, too!-----"I came, I saw, I started makinggames." ... If you'll excuseme, I must resume my searchfor my long lost lobotomy stitches.
Just to make sure I have it right...

The world transform moves all of the vertices in your buffer into the intended spot/scale/rotation in the world. It's the first to be multiplied by the vertices. This I think I'm pretty clear on.

The view transform... this I'm unsure about, so I'll do an example instead.
Say we have a triangle with vertices at (0,0,0),(1,0,0), and (0.5,1,0) (or something like that, it really doesn't matter), and the view transform is set to an identity. Now, if I were to apply a matrix with a transform of (-1,0,0) (1 unit left) to the view matrix, would that equate to moving everything in the scene one unit right (the illusion of a moving camera, I guess)?

The projection transform... setting up this one has completely confoozled me. Anyone care to give an explanation? (I've always just used whatever was in the DXSDK examples and never paid attention to it).

Thanks again,
Normie

[edited by - normie on March 19, 2002 10:02:38 AM]
I am a devout follower of the"Lazy Programmer's Doctrime"(tm)...and I'm damned proud of it, too!-----"I came, I saw, I started makinggames." ... If you'll excuseme, I must resume my searchfor my long lost lobotomy stitches.
No... a gemeric translation matrix of (-1,0,0) would look like :

{1 0 0 -1}
{0 1 0 0 }
{0 0 1 0 }
{0 0 0 1 }

So when your vertex (x,y,z,1) (d3d will add the one when multiplying the 3 element vector by the 4 element matrix, it''s necessary), is multiplied by the matrix, you get

x = x + 0 + 0 + -1 = x-1
y = 0 + y + 0 + 0 = y
z = 0 + 0 + z + 0 = z

so the left translation of -1 will do just that, move your vertex to the left(in screen units) by one unit.


To setup the View and Projection matrices properly you should worry less how they do what they do (for now), and instead look at the predefined functions provided for setting them up.

There are a variety of D3DX functions specficially for the purpose of setting up the view matrix and a variety for setting up the projection matrix. Browse through the DX SDK docs and check up on the D3DX math functions and you should find them.

The generic Translation, Rotation, and Scaling matrix functions, however, are NOT generally appropriate for manipulating the view and projection matrices. They ARE appropriate for the world matrix.
So, ok, wait... using a translation of (-1,0,0) on the view transform does the same thing as it would on the world transform?

....
......
*bzzt*

"Whoops, there it goes. Yep, my brain stopped." -Ed

Does this mean that the view and world transforms are _technically_ interchangeable? Or have I missed the point completely (again)?

-Normie

PS. Sorry if I've frustrated you all with my stupidity, but I feel the need to share.

[edited by - normie on March 19, 2002 12:08:29 PM]
I am a devout follower of the"Lazy Programmer's Doctrime"(tm)...and I'm damned proud of it, too!-----"I came, I saw, I started makinggames." ... If you'll excuseme, I must resume my searchfor my long lost lobotomy stitches.
The view matrix and the world matrix are not interchangable... but they both function in a similar manner. There are two varying factors that define the different matrices (World, View, and Perspective)

one, how the matrix is created.
two, the order of operations.


If you have a vertex in object space, FIRST you mutliply it by the world matrix. Then you take the result from that and multiply it by the view matrix. Then you take the result from THAT and multiply it by the perspective matrix. The reason the order is important is because matrix math is not communitative (A*B*c != B*A*c).

You shoudln't even consider trying to set up the view and perspective matrices yourself. Just use the functions provided in the sdk. Even when you understand how they work there is never a need to do the math yourself and set them up manually.


Your misconception is that you're moving some mystical viewer around, you aren't, you're just moving the vertices. In fact if you wanted the viewer (who's initial position can be considered 0,0,0 looking down the z axis) to be shifted one to the right (which would be 0,0,1 looking down the z axis)... THEN you'ld use the equivalent matrix of a (-1,0,0) translation as you did in your example. Don't actually do it that way (use the D3DX LookAt functions!), but the results would be the same. To move the viewer you don't ACTUALLY move the viewer, you move the vertices around the viewer.


In a simliar manner the perspective matrix then takes the resulting x,y values and perspective corrects them. I.e. makes the x and y values get smaller as z gets big.

Each of the three matrices is just a mechanism with which to perform mapping operations on the vertices.



[edited by - sorrow on March 19, 2002 12:40:04 PM]

This topic is closed to new replies.

Advertisement