Re: [Algorithms] 3D to 2D
Brought to you by:
vexxed72
|
From: Scott B. <Sc...@Ma...> - 2001-03-17 12:06:13
|
THanks Tom. While I've got you on the subject, I've got my 2D to 3D function
working, but it's a bit of a hack. What is the proper way to go about using
the Proj and View matrix to project a screen coor into 3D space if you don't
mind my asking? I just want to clean up my code. I'm currently using
something that looks like the code for my old 3Dto2D function which was
wacked..
----- Original Message -----
From: "Tom Forsyth" <to...@mu...>
To: <gda...@li...>
Sent: Saturday, March 17, 2001 5:58 AM
Subject: RE: [Algorithms] 3D to 2D
> There are three matrices to worry about - projection (P), world (W) and
> viewport (V). Once you have all of those, it's easy:
>
>
> camera_space_coord = W * original_vertex;
> clip_space_coord = P * camera_space_coord;
> // At this stage you can clip to -w<x<w, -w<y<w, 0<z<w.
> screen_pos_coord = V * clip_space_coord;
> screen_pos.X = screen_pos_coord.X / screen_pos_coord.W;
> screen_pos.Y = screen_pos_coord.Y / screen_pos_coord.W;
> screen_pos.Z = screen_pos_coord.Z / screen_pos_coord.W;
>
> (remember that vertices behind the camera will get projected to be in
front
> of the camera by this divide).
>
>
>
> Or, in D3D code:
>
> D3DXMATRIX matViewport;
> D3DVIEWPORT8 vpViewport;
> // The framework annoyingly hides the viewport from me.
> m_pd3dDevice->GetViewport ( &vpViewport );
> MakeMatrixFromViewport ( matViewport, vpViewport );
>
> D3DXMATRIX matTotal;
> D3DXMatrixMultiply ( &matTotal, &matWorld, &m_matView );
> D3DXMatrixMultiply ( &matTotal, &matTotal, &m_matProj );
> D3DXMatrixMultiply ( &matTotal, &matTotal, &matViewport );
>
>
> for ( each vertex )
> {
> D3DXVECTOR4 pPos;
> // vPos is the original vertex position in object-space.
> D3DXVec3Transform ( &pPos, &vPos, &matTotal );
> // And do the homogenous thing.
> // I do it this way, instead of using D3DXVec3TransformCoord,
> because the
> // sign of the W tells me if the vert was behind the camera or not.
> pPos.x /= pPos.w;
> pPos.y /= pPos.w;
> pPos.z /= pPos.w;
> }
>
>
>
>
> Getting world and projection matrices should be trivial - that's what you
> pass to D3D. Finally, MakeMatrixFromViewport is this:
>
>
> #if DXVERSION==8
> static void MakeMatrixFromViewport ( D3DMATRIX &matViewport, D3DVIEWPORT8
> &vpViewport )
> #else
> static void MakeMatrixFromViewport ( D3DMATRIX &matViewport, D3DVIEWPORT7
> &vpViewport )
> #endif
> {
>
> #if DXVERSION==8
> float fHalfWidth = vpViewport.Width * 0.5f;
> float fHalfHeight = vpViewport.Height * -0.5f;
> #else
> float fHalfWidth = vpViewport.dwWidth * 0.5f;
> float fHalfHeight = vpViewport.dwHeight * -0.5f;
> #endif
>
> matViewport._11 = fHalfWidth;
> matViewport._12 = 0.0f;
> matViewport._13 = 0.0f;
> matViewport._14 = 0.0f;
>
> matViewport._21 = 0.0f;
> matViewport._22 = fHalfHeight;
> matViewport._23 = 0.0f;
> matViewport._24 = 0.0f;
>
> #if DXVERSION==8
> matViewport._31 = 0.0f;
> matViewport._32 = 0.0f;
> matViewport._33 = vpViewport.MaxZ - vpViewport.MinZ;
> matViewport._34 = 0.0f;
>
> matViewport._41 = vpViewport.X + fHalfWidth;
> matViewport._42 = vpViewport.Y - fHalfHeight;
> matViewport._43 = vpViewport.MinZ;
> matViewport._44 = 1.0f;
> #else
> matViewport._31 = 0.0f;
> matViewport._32 = 0.0f;
> matViewport._33 = vpViewport.dvMaxZ - vpViewport.dvMinZ;
> matViewport._34 = 0.0f;
>
> matViewport._41 = vpViewport.dwX + fHalfWidth;
> matViewport._42 = vpViewport.dwY - fHalfHeight;
> matViewport._43 = vpViewport.dvMinZ;
> matViewport._44 = 1.0f;
> #endif
> }
>
>
>
>
> As far as I know, the maths is identical for OpenGL, modulo some
row/column
> swapping.
>
> Enjoy.
>
>
>
> Tom Forsyth - Muckyfoot bloke.
>
> What's he up to now?
> http://www.muckyfoot.com/startopia/cam.html
>
>
>
> > -----Original Message-----
> > From: Scott Bean [mailto:Sc...@Ma...]
> > Sent: 17 March 2001 10:16
> > To: gda...@li...
> > Subject: [Algorithms] 3D to 2D
> >
> >
> > I'm having a fun time trying to accuratly convert a Point in
> > 3D world space
> > to 2D screen coordinates with depth, given a projective view type and
> > field-of-view and viewport width and height.
> >
> > this works fine with a 90 degree field, but is off when changing the
> > field...
> >
> > Convert3Dto2D(int* x, int* y, float* z, float* Depth,
> > MADVECTOR Vector)
> > {
> > //x & y being screen coordinates, z being in camera
> > space, and depth
> > being |Camera-Vector|.
> >
> > //Convert to camera space
> > GetCamera()->WorldToFrame(&Vector);
> >
> > *z = Vector.z;
> > *x = (int)( (float)Width / 2 * ( 1 + Vector.x / Vector.z *
> > (float)tan(GetCameraField()/2) / Aspect ) );
> > *y = (int)( (float)Height / 2 * ( 1 - Vector.y / Vector.z *
> > (float)tan(GetCameraField()/2)) );
> > *Depth = MADVectorGetLength(Vector);
> > }
> >
> > I"m a little bit confused when it comes to the projection
> > matrices and stuff
> > and use D3DX for creating them so I dont' have a whole
> > understanding of what
> > I'm doing wrong.
> >
> > Any help or sarcasm would be greatly appreciated :)
> > Thanks
> >
> >
> > _______________________________________________
> > GDAlgorithms-list mailing list
> > GDA...@li...
> > http://lists.sourceforge.net/lists/listinfo/gdalgorithms-list
> >
>
> _______________________________________________
> GDAlgorithms-list mailing list
> GDA...@li...
> http://lists.sourceforge.net/lists/listinfo/gdalgorithms-list
>
|