Skip to main content
GameDev.net gamedev.net
🔒 Locked

Understanding Matrix Views

Started by programmermattc Jul 24, 2006 at 9:00 PM 4 replies 1.3k views
Original Post
programmermattc
programmermattc
So I've been working with DirectX and following the included tutorials. The third tut introduces matrices. The code (without a spinning matWorld) is:

VOID SetupMatrices(float whereis)
{
	D3DXVECTOR3 vEyePt(whereis, 3.0f, -5.0f);
	D3DXVECTOR3 vLookAt(whereis, 0.0f, 0.0f);
	D3DXVECTOR3 vUpVec(0.0f, 1.0f, 0.0f);
	
	D3DXMATRIXA16 matView;
	D3DXMatrixLookAtLH(&matView, &vEyePt, &vLookAt, &vUpVec);
	g_pDevice->SetTransform(D3DTS_VIEW, &matView);

	D3DXMATRIXA16 matProj;
	D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f);
	g_pDevice->SetTransform(D3DTS_PROJECTION, &matProj);
}
I guess I understand the EyePt, the LookAt, and the UpVec though a lot of the rest if confusing. Can anyone explain matrices in game programming a bit more or know a good resource about them? (I'll also be looking around in the DX MSDN site to understand what the functions do)
=============================RhinoXNA - Easily start building 2D games in XNA!Projects
Driv3MeFar
Driv3MeFar
Eye, lookat, and up are used to construct your view-frame coordinate system, which, basically, can be constructed as follows:
LookAt becomes (in the DX left-handed coord frame) your cameras +Z axis.
The cross product between your globabl Up vector and lookAt will get your cams +X-axis, and lastely, Z cross X gets your cams +Y-axis.
Then all that is left is to set your camera as the origin of its local frame, which involves taking the inverse of the transaltion matrix which got your camera to its (Eye) position.
Combining these into a single matrix will define your view frame coordinate system, which is the job of the View matrix:
| x1 x2 x3 -x*at |
| y1 y2 y3 -y*at |
| z1 z2 z3 -z*at |
| 0 0 0 1 |
Where * denotes the dot product.

Setting this matrix will effectively center your camera at (Eye) looking in the direction of (LookAt).

The projection matrix defines your view frustrum. The derivation is slightly more complex, so I suggest you look it up on Wikipedia or something, because I'll likely screw it up.
programmermattc
programmermattc
What I have above is a passed variable called 'whereis' I have a global float g_pWhereIs (just for testing) and increase it by 20.0 before calling my SetupMatrices, shouldn't that move the object (in my view) if I increase my EyePt X and my LookAt X by 20.0 each time I press VK_LEFT?
=============================RhinoXNA - Easily start building 2D games in XNA!Projects
Driv3MeFar
Driv3MeFar
The view matrix does not actualy move objects, but rather, moves your camera. You should see the camera move left along the +X axis, so your objects will appear to have moved right (though they havent really moved at all). To move your objects, you have to build a translation matrix for each on of them.
Peachy keen
Peachy keen
Personally have always found the matrix pre-packaged functions to be a rather large contribution to the problems involved in understanding how your things end up looking how they do. I HIGHLY advise learning some linear algebra, especially some stuff about the multiplication of matrices, and read the SDK about the order inwhich the mat's are multiplied together, and the range of end-values that are considered 'visible'. Do some hand-made trials, and hand write a few simple matrices to get things to look how you think they should.

If you understand the matrix, learning whatever little stupid function to help actually put the matrix together won't seem so magic, and you won't feel so locked down by mysterious variables and values.

This is expecially true when dealing with the D3DX stuff, since [at least a huge number of the math functions] describe exactly the process they use to get the end result, so you can see exactly if its really doing what you intend it to do.
Troll
Troll
I don't agree so much with that advice. Almost all of what I learned in my linear algebra class would be of little use in understanding what the OP is confused with. Gaussian elimination is of little application in a scene graph. Knowing the mechanics of multiplying one matrix's ith row with the ith column of the other really provides me with no "feel" of what's going in the geometrical interpretation of matrices used in 3D games. Here are the two books I found with the best discussion of the geometric application of matrices:

Mathematical Elements for Computer Graphics
Jim Blinn's Corner

Most of the Blinn's book isn't about matrices specifically, but I found his discussion of building the look-at matrix to be a very, very valuable discussion of matrices in 3D graphics.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.