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

Projection Matrix

Started by GrahamZ May 17, 2004 at 7:44 AM 2 replies 800+ views
Original Post
GrahamZ
GrahamZ
Hi. I''m learning DirectX 8 and I''m trying to create my own version of D3DXMatrixPerspectiveFovLH(). What I''ve got is this: //Create a left-handed projection matrix. matrix4 createProjectionMatrixLH(matrix4 &mOut, float fovy, float Aspect, float znear, float zfar) { ZeroMemory(&mOut, sizeof(matrix4)); if(sinf(fovy/2.0f)) { float w = cosf(fovy/2.0f) / sinf(fovy/2.0f) * Aspect; float h = cosf(fovy/2.0f) / sinf(fovy/2.0f); float Q = zfar / (zfar - znear); mOut._11 = w; mOut._22 = h; mOut._33 = Q; mOut._34 = 1.0f; mOut._43 = -Q * znear; } return mOut; } But the image it''s giving me is stretched horizontally. Also: I''m running it in windowed mode and before I could resize the client area without it distorting the image, now it does. The only time the image looks right is if I make the client area square. I''m still finding Aspect the same way as when I was using the D3DXMatrixPerspectiveFovLH() function; and the debugger is showing me what seems to be the correct value for Aspect... I don''t know what''s going on? GrahamZ.
don
don
Change this:

float w = cosf(fovy/2.0f) / sinf(fovy/2.0f) * Aspect;

To this:

float w = (cosf(fovy/2.0f) / sinf(fovy/2.0f)) / Aspect;
S1CA
S1CA
Good call don.

Although the check isn''t really necessary at all, I''d also make the "if (sinf(fovy/2.0f))" binary comparison of a float value safer (IEEE floats should always be considered to be approximations rather than absolute) with something like:

"if (fabsf(sinf(fovy/2.0f)) >= FLT_EPSILON)"


Simon O''Connor
Game Programmer &
Microsoft DirectX MVP
Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site
GrahamZ
GrahamZ
Thankyou both. I''ve made the changes and it''s working fine now. I take it, since I''ve been passing the function: ScreenAspect = (float)clientRect.right/clientRect.bottom till now, that D3DXMatrixPerspectiveFovLH() has "/ Aspect"? I''ve decided to pass the reciprocal ScreenAspect, and keep w the familiar way.

Thanks again, GrahamZ.

Topic Locked

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

Sign in to reply to this topic.