C++中用于透视投影矩阵的函数

4

有没有能够在C++中返回3x3矩阵透视投影的函数?

Matrix Perspective()
{
   Matrix m(0, 0, 0);  // Creates identity matrix
   // Perspective projection formulas here
   return m;
}

1
你的矩阵代码是什么?这看起来不像一个单位矩阵。 - rlbond
2
透视投影作用于向量,而不是矩阵。因此,我不确定您在问什么。 - Keith Randall
1
此外,透视投影需要参数,如FOV、长宽比、近和远观看距离,并必要返回一个同质化的4x4矩阵。所以我更加困惑于你要使用3x3矩阵做什么。 - kidnamedlox
2个回答

7

这里有一个返回4x4矩阵的函数,使用OpenGL gluPerspective man page中的公式:

static void my_PerspectiveFOV(double fov, double aspect, double near, double far, double* mret) {
    double D2R = M_PI / 180.0;
    double yScale = 1.0 / tan(D2R * fov / 2);
    double xScale = yScale / aspect;
    double nearmfar = near - far;
    double m[] = {
        xScale, 0, 0, 0,
        0, yScale, 0, 0,
        0, 0, (far + near) / nearmfar, -1,
        0, 0, 2*far*near / nearmfar, 0 
    };    
    memcpy(mret, m, sizeof(double)*16);
}

0

使用OpenCV 2.0,你几乎可以实现你的伪代码。

有一个矩阵类Mat和透视变换的perspectiveTransform。而Mat::eye则返回一个单位矩阵。

我链接的文档是针对OpenCV 1.1(使用C语言),但是从手册中很容易推断出在OpenCV 2.0(使用Mat类)中正确的用法。


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