This topic is now archived and is closed to further replies.

Modifying D3DUtil_SetProjectionMatrix for ortho

This topic is 6167 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

HRESULT D3DUtil_SetProjectionMatrix( D3DMATRIX& mat, FLOAT fFOV, FLOAT fAspect,
                                     FLOAT fNearPlane, FLOAT fFarPlane )
    if( fabs(fFarPlane-fNearPlane) < 0.01f )
        return E_INVALIDARG;
    if( fabs(sin(fFOV/2)) < 0.01f )
        return E_INVALIDARG;

//  FLOAT w = fAspect * ( cosf(fFOV/2)/sinf(fFOV/2) );

//  FLOAT h =   1.0f * ( cosf(fFOV/2)/sinf(fFOV/2) );

//  FLOAT Q = fFarPlane / ( fFarPlane - fNearPlane );

  FLOAT w =   1.0f * ( cosf(fFOV/2)/sinf(fFOV/2) );
  FLOAT h =   1.0f * ( cosf(fFOV/2)/sinf(fFOV/2) );
  FLOAT Q =   1.0f / ( fFarPlane - fNearPlane );

    ZeroMemory( &mat, sizeof(D3DMATRIX) );
//  mat._11 = w;

//	mat._22 = h;

//  mat._33 = Q;

//  mat._34 = 1.0f;

//  mat._43 = -Q*fNearPlane;

	mat._11 = 2.0f/w;
	mat._22 = 2.0f/h;
	mat._33 = Q;
	mat._43 = -(fNearPlane * Q);
	mat._44 = 1.0f;

    return S_OK;
I'm directly modifying the SetProjectionMatrix routine just because it simplifies things greatly. It's almost correct. I got the equations off of an older thread posted by an Anon. That person left off the w and h equations and I was wondering if anyone knows what they should be. It mostly works as it is in that there's no Z but X and Y screen positions are still being incorrectly calculated. Thanks, Ben [/source] Edited by - KalvinB on January 25, 2001 2:15:58 PM

Share this post

Link to post
Share on other sites
I believe those are DX8 only. I''m using DX7. Also modifying the original code directly eliminates having to rewrite or add more code than I need to the game. It also gives me the opportunity to do any optimizing that might beable to be done.

Besides I''ve got most of the equations. It''s just barely off when I test it out. It''d be easier to finish up doing it this way than by researching a whole new method.


Share this post

Link to post
Share on other sites
1st - both functions are present in DX7, just check! (in D3DX)!!

2nd - in case you still do not trust those helpers, openGL defines an ortho projection matrix as follows:

a 0 0 tx
0 b 0 ty
0 0 c tz
0 0 0 -1

a = 2/(right-left)*fAspect = fAspect
b = 2/(top-bottom) = 1
c = -1/(far-near)
tx= -(right+left)/(right+left) = 0
ty= -(top+bottom)/(top-bottom) = 0
tz= -(far+near)/(far-near) = -1

right = -left = 1
top = -bottom = 1
near = 0
far = whatever

source: Microsoft (yes!)

Share this post

Link to post
Share on other sites
Thanks but I already knew about the OpenGL functions at MS. D3DX is just more code to add to a program that has as much as it needs concerning 3D functions.

at my site

under "Tricks of the game programming newbies" you''ll find a tutorial I wrote on doing orthogonal by modifying D3DUtil_SetProjectionMatrix directly. Which is easier once you know how to do it. So yeah, I figured it out. It just took actually working through the equations. Also since I know how it works, I can optimize as much as possible.

Thanks for looking into it though,


Share this post

Link to post
Share on other sites