Projection Matrix Woes

Started by
4 comments, last by Goblin 22 years, 4 months ago
I''m having a lot of trouble with getting a correct perspective projection/view matrix aligned for my direct3d 8 program. I want the camera to be 16 units wide by 12 units tall. If I want 0,0 to be in the lower left and 16,12 to be in the upper right, I assume to put the camera at 8,6, but it''s the z-coordinates of both the camera and the near and far plane of the projection matrix that are giving me a lot of grief. I imagine there''s also a way to control how steep the perspective is, any tips would be appreciated. Thanks! ----------------- The Goblin (madgob@aol.com) ----------------- "Before critisizing somebody, walk a mile in their shoes. That way, when you do critisize them, not only will you be a mile away, but you''ll also have their shoes!"
- The Goblin (madgob@aol.com)
Advertisement
Your requirement is still missing something. At what z level do you want the 0,0 to be on the lower left? Specifying your camera''s z location and FOV depends on this. If, for example, we wanted (0,0,0) to be on the lower left, then we can choose our camera''s location to be (8,6,-4) for example, and choose our FOVx to be taninv(8/4). If we want the perspective to be more steep, then we can choose our camera''s location to be, say, (8,6,-2) and choose the FOVx to be taninv(8/2), and so on.
The z clipping planes has nothing to do with perspective. The clipping planes'' functionality is to choose the visible area of your scene as well as specifying the way real z values are distributed along the z buffer range. Choose any two values for them that makes your objects visible. Keep in mind that the bigger the difference of the two z values, the higher the posibility of z buffering inaccuracy.
Final advice. Do not choose Zn to be 0. If necessary, choose Zn to be 0.1, 0.001, anything other than 0 because specifying 0 disables the z buffering by causing all the objects to have Zs as 1. However, if you''re using w buffering, no such problem occurs.
The general way of doing things is to have the camera pointing down the Z axis. You than transform the camera based on its position, and update the world by normalizing the camera. see www.mrgamemaker.com

-Matt
Sorry if I wasn''t clear enough... now that I think about it, I''d probably want the lower left to be at 0,0,-1 and the upper right to be at 16,12,-1. And I''ve been playing with D3DXMatrixPerspectiveLH to no avail, but if there is a way to do it with D3DXMatrixPerspectiveFovLH that also works.

And in my playing with things, playing with the near Z plane makes things bigger or smaller, and playing with the far makes them either visible or not visible. What''s really strange is that the only way I''ve gotten it to work properly so far is to place the camera at say 8,6,-10 and then make the projection have a width of 16, a height of 12, a near plane of 10, and a far plane of 5... why this works I don''t know, but I would prefer to if possible have a more reliable matrix in which I could control how steep the perspective is (and actually know what I''m doing).

-----------------
The Goblin (madgob@aol.com)
-----------------
"Before critisizing somebody, walk a mile in their shoes. That way, when you do critisize them, not only will you be a mile away, but you''ll also have their shoes!"
- The Goblin (madgob@aol.com)
Aha, now I understand what''s happening with you. Actually, what affects the perspective level is the FOV, nothing else. The wider the field of view, the more steep is the perspective. The effect is not like stretching because you''re mapping a spherical image onto a plane surface (your moniter). So with wider FOV, bigger part of the sphere will be mapped on your plane surface causing the perspective effect to be increased. In your case, you''re using D3DXMatrixPerspectiveLH, not D3DXMatrixPerspectiveFovLH. D3DXMatrixPerspectiveLH calculates the FOV from the given parameters. i.e. FOVx = taninv(w/Zn). So, both of these values affect the FOV. Zn will affect the FOV as well as affecting the clipping region. If you use D3DXMatrixPerspectiveFovLH, you''ll find that Zn never affects the perspective level and that FOV is the only parameter that affects it. With the values that you''ve specified, as I mensioned before, Zf is not included in calculating the FOV so putting it to 5 didn''t affect the FOV but it will no doubt affect the clipping region as well as Z buffering. However, if you return to the documentation of DX, you''ll find the mathematical equations necessary to calculate all these things. You can try to calculate it manually in order to know how did it work. It seems that DX do not check the perspective matrix, it only multiplies it. So everything is in control of mathematics. Giving unsensible values do not give you any error, it only gives you unpredictable results.

This topic is closed to new replies.

Advertisement