Sign in to follow this  

Convert to Viewport Coordinates

Recommended Posts

How do I Convert a point in model world coordinates to window coordinates? Sorry, It sounds obvious. But the fact is this: I have a point given in world coordinates: P = {x1, y1, z1} The main task is to convert that point to viewport coordinates, The viewport has the following rules: struct VIEWPORT { float m_left; /// =0 minimun value float m_right; ///=1.0f maximun value float m_top; //=0 minimun value float m_bottom; //=1.0f maximun value float m_znear; ///=0.0f minimun value float m_zfar; ///=1.0f maximun value } The camera system is given as a 4x4 matrix: C = | l1 u1 d1 t1| | l2 u2 d2 t2| | l3 u3 d3 t3| | 0 0 0 1 | At last, the projection matrix Proj is calculated as following: /* l is Frustum left = -0.5 r is Frustum Right = 0.5 t is Frustum Top = 0.5 b is Frustum Bottom = -0.5 zn is Frustum Near = 1.0 zf is Frustum Far = 100.0 */ Matrix4Zero(Proj ); ///convert to 0 matrix Proj._11 =2*zn/(r-l); Proj._22 =2*zn/(t-b); Proj._13 =(r+l)/(r-l); Proj._23 =(t+b)/(t-b); Proj._33 =-(zf+zn)/(zf-zn); Proj._43 =-1; Proj._34 =-2*zn*zf/(zf-zn); Well, at this point, I think that to convert a point to viewport coordinates I have to do this: Cinv = Matrix4Inverse(C); //convert point to camera space Pc = Cinv*P; ///project point to screen Pv = Proj * Pc So, I expect that Pv.x is in range of {0,1} Pv.y is in range of {0,1}, inverted Pv.z is in range of {0,1} But It doesn't Happen! Pv has strange values, and I don't understand why, If C and Proj were the matrices that I pass to OpenGL and they work fine, but when I try to do the transformation by hand, It doesn't work! Thanks.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this