Advertisement

Projection matrix & 3d to 2d conversion

Started by August 29, 2001 10:19 PM
0 comments, last by Rocket05 23 years, 5 months ago
hey im trying to make a 3d math library, to do so i need all the basic matricies used in a 3d application, such as world/view/projection. i allready have view and world maticies, but i need a projection matrix for perspective projection. also, is the projection matrix used to convert 3d coords to 2d or after the transformation''s is vector math used to do it? (actually writing x = x/z; y = y/z;, etc) if anybody knows the correct perspective projection matrix and how to convert 3d to 2d coords, anyhelp is greatly appreciated thanks
"I never let schooling interfere with my education" - Mark Twain
[This would probably be more appropriately asked in the Graphics forum]

I assume you want perspective projection (rather than parallel/orthographic), and I assume you''re using a left handed coordinate system and column major matrices:

  void Matrix::SetProjection(float near, float far, float fov, float aspect){    float    w, h, Q;	    w = aspect * ( cosf(fov/2)/sinf(fov/2) );    h = 1.0f * ( cosf(fov/2)/sinf(fov/2) );    Q = far / ( far - near );    m_mat._11 = w;    m_mat._22 = h;    m_mat._33 = Q;    m_mat._34 = 1.0f;    m_mat._43 = -Q*near;}  


near is the near z clip plane, far is the far z clip plane, fov is the field of view and aspect is the aspect ratio


The projection matrix is usually done as a 4x4 "homogeneous" matrix where the vector you pass in has another component called W, so your vertex is:

x,y,z,w

For projection purposes the W of each vector to be transformed is usually set to 1

the 4D vector (xyzw) is then transformed by the 4x4 (homogeneous) projection matrix (see above)

Finally to convert back to non-homogeneous form you divide by W:

v.x = source.x
v.y = source.y
v.z = source.z
v.w = 1

vscreen = v * mProjectionMatrix;

vscreen.x = vscreen.x / vscreen.w;
vscreen.y = vscreen.y / vscreen.w;
vscreen.z = vscreen.z / vscreen.w;


vscreen.z is used for z buffering

vscreen.x and vscreen.y need to be offset using a viewport to get them into final 2D device coordinates


A book I definately recommend you buy/borrow is "Realtime Rendering", it''s got great info on this sort of stuff presented in a non-intimidating way for non-mathematicians.

--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

This topic is closed to new replies.

Advertisement