views:

51

answers:

1

I'm utilising some 2d / 3d tracking data (provided by pfHoe) to help integrate some 3d models into the playback of some 2d video.

Things are working.... okay... but there's still some visible 'slipping' of the models against the video background and I suspect this is may be because the XNA CreatePerspective helper method isn't taking into account some of the additional data supplied by pfHoe such as independent horizontal / vertical field of view angles and focal length.

Would anyone be able to point me towards some examples of constructing view matrices that include such details?

+1  A: 

This MSDN article shows you how to write a method which creates a perspective matrix that takes into account horizontal and vertical FOV independently (albeit, in C++)
http://msdn.microsoft.com/en-us/library/bb147302%28VS.85%29.aspx

D3DXMATRIX 
ProjectionMatrix(const float near_plane, // Distance to near clipping 
                                         // plane
                 const float far_plane,  // Distance to far clipping 
                                         // plane
                 const float fov_horiz,  // Horizontal field of view 
                                         // angle, in radians
                 const float fov_vert)   // Vertical field of view 
                                         // angle, in radians
{
    float    h, w, Q;

    w = (float)1/tan(fov_horiz*0.5);  // 1/tan(x) == cot(x)
    h = (float)1/tan(fov_vert*0.5);   // 1/tan(x) == cot(x)
    Q = far_plane/(far_plane - near_plane);

    D3DXMATRIX ret;
    ZeroMemory(&ret, sizeof(ret));

    ret(0, 0) = w;
    ret(1, 1) = h;
    ret(2, 2) = Q;
    ret(3, 2) = -Q*near_plane;
    ret(2, 3) = 1;
    return ret;
}   // End of ProjectionMatrix

The math is further explained in the article linked above to aid you in writing a C# version of this method. Good luck :-)

Joel Martinez
Hmmm... I may have to a little more as my first interpretation of it in C# gives some... odd... results... :)private Matrix CreateProjection(float horizontalFov, float verticalFov, float nearPlane, float farPlane) { var w = 1f / (float)Math.Tan(horizontalFov * 0.5f); var h = 1f / (float)Math.Tan(verticalFov * 0.5f); var q = farPlane / (farPlane - nearPlane); return new Matrix(w, 0, 0, 0, 0, h, 0, 0, 0, 0, q, 1, 0, 0, -q * nearPlane, 0); }
lzcd