如何构建透视投影矩阵(无 API)

21
我开发了一个简单的3D引擎(没有使用任何API),已经成功地将我的场景转换到世界和视图空间,但是在使用透视投影矩阵(类似于OpenGL)将场景(从视图空间)投影时遇到了问题。我不确定fov、near和far值,而且得到的场景有畸变。我希望有人可以指导我如何正确构建和使用透视投影矩阵,并提供一些代码示例。非常感谢您提供的任何帮助。
矩阵构建:
double f = 1 / Math.Tan(fovy / 2);
return new double[,] { 

    { f / Aspect, 0, 0, 0 },
    { 0, f, 0, 0 },
    { 0, 0, (Far + Near) / (Near - Far),  (2 * Far * Near) / (Near - Far) }, 
    { 0, 0, -1, 0 } 
};

矩阵的使用:

foreach (Point P in T.Points)
{     
    .
    .     // Transforming the point to homogen point matrix, to world space, and to view space (works fine)
    .     

    // projecting the point with getProjectionMatrix() specified in the previous code :      

    double[,] matrix = MatrixMultiply( GetProjectionMatrix(Fovy, Width/Height, Near, Far) , viewSpacePointMatrix );

    // translating to Cartesian coordinates (from homogen):

    matrix [0, 0] /= matrix [3, 0];
    matrix [1, 0] /= matrix [3, 0];
    matrix [2, 0] /= matrix [3, 0];
    matrix [3, 0] = 1;
    P = MatrixToPoint(matrix);

    // adjusting to the screen Y axis:

    P.y = this.Height - P.y;

    // Printing...
}

我也推荐之前的课程(投影点和3D视图)。http://www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-orthographic-projection-matrix - user18490
2个回答

33

以下是一种典型的透视投影矩阵实现方法。 这里有一个很好的链接,可以解释所有内容:OpenGL投影矩阵

void ComputeFOVProjection( Matrix& result, float fov, float aspect, float nearDist, float farDist, bool leftHanded /* = true */ )
{
    //
    // General form of the Projection Matrix
    //
    // uh = Cot( fov/2 ) == 1/Tan(fov/2)
    // uw / uh = 1/aspect
    // 
    //   uw         0       0       0
    //    0        uh       0       0
    //    0         0      f/(f-n)  1
    //    0         0    -fn/(f-n)  0
    //
    // Make result to be identity first

    // check for bad parameters to avoid divide by zero:
    // if found, assert and return an identity matrix.
    if ( fov <= 0 || aspect == 0 )
    {
        Assert( fov > 0 && aspect != 0 );
        return;
    }

    float frustumDepth = farDist - nearDist;
    float oneOverDepth = 1 / frustumDepth;

    result[1][1] = 1 / tan(0.5f * fov);
    result[0][0] = (leftHanded ? 1 : -1 ) * result[1][1] / aspect;
    result[2][2] = farDist * oneOverDepth;
    result[3][2] = (-farDist * nearDist) * oneOverDepth;
    result[2][3] = 1;
    result[3][3] = 0;
}

抱歉,这里的“uh”和“uw”是什么意思?用户宽度和用户高度吗? - ReX357
2
@ReX357 uw表示近/右,uh表示近/上,其中右是右剪裁平面的坐标,上是顶部剪裁平面的坐标。由于上述透视投影是对称的,因此right = 水平宽度的一半,top = 垂直高度的一半,然后uw/uh = top/right = 高度/宽度 = 1/aspect。 - Wayne Wang
在OpenGL中,z通常是乘以-1吗? - user755921
@racarate 我认为OpenGL使用右手坐标系,而DirectX使用左手坐标系。如果你将OpenGL转换为左手坐标系以与DirectX保持一致,那么你需要将z乘以-1。上述计算已经考虑到了这一点。(你传递给函数的leftHand参数) - Wayne Wang
@WayneWang,我正在阅读你的链接,但是我卡在了“接下来,我们将xp和yp与NDC的xn和yn进行线性关系映射;[l,r]⇒[-1,1]和[b,t]⇒[-1,1]。”这里是方程式,你能解释一下为什么我们要添加beta以及为什么我们要用xp = r和xn = 1替换吗? - watashiSHUN
@watashiSHUN,这是一个多步骤的过程,用于说明如何计算透视投影。xp,yp是投影在近平面上的点,其范围为(l,r),NDC空间范围为(-1,1)。因此,线性映射被应用。添加beta是因为视锥体可能是不对称的。通常,视锥体是对称的,因此beta等于0。您可以通过继续阅读该文章来获取此信息。 - Wayne Wang

3

另一个可能有用的功能。

这个功能基于左/右/上/下/近/远参数(在OpenGL中使用):

static void test(){
    float projectionMatrix[16];

    // width and height of viewport to display on (screen dimensions in case of fullscreen rendering)
    float ratio = (float)width/height;
    float left = -ratio;
    float right = ratio;
    float bottom = -1.0f;
    float top = 1.0f;
    float near = -1.0f;
    float far = 100.0f;

    frustum(projectionMatrix, 0, left, right, bottom, top, near, far);

}

static void frustum(float *m, int offset,
                     float left, float right, float bottom, float top,
                     float near, float far) {

    float r_width  = 1.0f / (right - left);
    float r_height = 1.0f / (top - bottom);
    float r_depth  = 1.0f / (far - near);
    float x =  2.0f * (r_width);
    float y =  2.0f * (r_height);
    float z =  2.0f * (r_depth);
    float A = (right + left) * r_width;
    float B = (top + bottom) * r_height;
    float C = (far + near) * r_depth;
    m[offset + 0] = x;
    m[offset + 3] = -A;
    m[offset + 5] = y;
    m[offset + 7] = -B;
    m[offset + 10] = -z;
    m[offset + 11] = -C;
    m[offset +  1] = 0.0f;
    m[offset +  2] = 0.0f;
    m[offset +  4] = 0.0f;
    m[offset +  6] = 0.0f;
    m[offset +  8] = 0.0f;
    m[offset +  9] = 0.0f;
    m[offset + 12] = 0.0f;
    m[offset + 13] = 0.0f;
    m[offset + 14] = 0.0f;
    m[offset + 15] = 1.0f;

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接