c++ - opengl 的投影和正交矩阵

标签 c++ c opengl matrix

我正在编写这段代码来设置 opengl 投影矩阵,但我得到了奇怪的结果,并希望获得一些帮助。

void mat4Projection(mat4* out, double r, double l, double t, double b, double n, double f);
void mat4OrthoProjection(mat4* out, double r, double l, double t, double b, double n, double f);

void mat4Projection(mat4* out, double r, double l, double t, double b, double n, double f)
{
    double data[] = { (2*n)/(r-l),  0,           (r+l)/(r-l),        0,
                       0,          (2*n)/(t-b),  (t+b)/(t-b),        0,
                       0,           0,           (-(f+n))/(f-n),     (-2*(f*n))/(f-n),
                       0,           0,           -1,                 1                };
    copyMatrix(out, data);
}

void mat4OrthoProjection(mat4* out, double r, double l, double t, double b, double n, double f)
{
    double data[] = { (2)/(r-l),  0,          0,           -((r+l)/(r-l)),
                       0,         (2)/(t-b),  0,           -((t+b)/(t-b)),
                       0,         0,          (-2)/(f-n),  -((f+n)/(f-n)),
                       0,         0,          0,           1              };
    copyMatrix(out, data);
}

我从这个链接http://www.songho.ca/opengl/gl_projectionmatrix.html得到了这个代码

最佳答案

和我的比较一下。它是标准代码并且运行良好,但您应该知道的一件事是它基于行 vector 。

void Matrix_OrthoProjection( Matrix& out_M, const __VERTEX__TYPE__ width, const __VERTEX__TYPE__ height, const __VERTEX__TYPE__ nZ, const __VERTEX__TYPE__ fZ)
{
    // asumed r-l = width , t-b = height
    out_M.s[_0x0_] = 2./width; out_M.s[_0x1_] = 0;              out_M.s[_0x2_] = 0;                    out_M.s[_0x3_] = 0;
    out_M.s[_1x0_] = 0;        out_M.s[_1x1_] = 2./height;      out_M.s[_1x2_] = 0;                    out_M.s[_1x3_] = 0;
    out_M.s[_2x0_] = 0;        out_M.s[_2x1_] = 0;              out_M.s[_2x2_] = -2./(fZ-nZ);          out_M.s[_2x3_] = 0;
    out_M.s[_3x0_] = 0;        out_M.s[_3x1_] = 0;              out_M.s[_3x2_] = -(fZ+nZ)/(fZ-nZ);     out_M.s[_3x3_] = 1.;
}

void Matrix_PerspectiveProjection
(Matrix& out_M,
 const __VERTEX__TYPE__ FOV,
 const __VERTEX__TYPE__ ASPECT,
 const __VERTEX__TYPE__ NEAR,
 const __VERTEX__TYPE__ FAR)
{

    float fov = 1.0f / (float)tan(FOV * 0.5f); 
    float nf = 1.0f / (NEAR - FAR);

    out_M.s[_0x0_] = fov/ASPECT;
    out_M.s[_1x0_] = 0;
    out_M.s[_2x0_] = 0;
    out_M.s[_3x0_] = 0;

    out_M.s[_0x1_] = 0.0;
    out_M.s[_1x1_] = fov;
    out_M.s[_2x1_] = 0.0;
    out_M.s[_3x1_] = 0.0;

    out_M.s[_0x2_] = 0.0;
    out_M.s[_1x2_] = 0.0;
    out_M.s[_2x2_] = (NEAR+FAR) * nf;
    out_M.s[_3x2_] = (2.f*NEAR*FAR) * nf;

    out_M.s[_0x3_] = 0.0;
    out_M.s[_1x3_] = 0.0;
    out_M.s[_2x3_] = -1.0;
    out_M.s[_3x3_] = 0.0;

}

也请引用此图片。

正交 enter image description here

观点 enter image description here

关于c++ - opengl 的投影和正交矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31839119/

相关文章:

user-interface - OpenGL 3D GUI 引擎和/或 Assets

c++ - 强类型 using 和 typedef

c++ - 容器对象标识的相等而不是元素明智

无法让 C 中的 while 函数工作

c++ - 黑人 : processor grids that do not use all MPI cores

c - 单个声明中多个声明符的初始化顺序

C++ 密码尝试限制

c++ - 带有 std::function 的通用 lambda 不捕获变量

cocoa - 创建 NSOpenGLView 时出错

opengl - 为什么我必须计算模型矩阵的逆矩阵的转置才能计算反射纹理的法线?