如何将本征矩阵和非本征矩阵转换为opengl投影并正确查看矩阵

时间:2018-12-02 11:33:50

标签: c++ opengl glm-math

我在原点,相机固有和非固有矩阵附近有一些3D点。我可以通过opencv中的projectPoints(使用x =内在*外在* X)函数获得正确的2D点。但是,当我想使用opengl渲染这些3D点时,它不能很好地工作。 我只是将内在矩阵转换为glm mat4,如下所示:

proj[0][0] = 2 * fx / width;
proj[1][0] = 0.0f;
proj[2][0] = (2 * cx - width) / width;
proj[3][0] = 0.0f;
proj[0][1] = 0.0f;
proj[1][1] = -2 * fy / height;
proj[2][1] = (height - 2 * cy) / height;
proj[3][1] = 0.0f;
proj[0][2] = 0.0f;
proj[1][2] = 0.0f;
proj[2][2] = -(far_clip + near_clip) / (near_clip - far_clip);
proj[3][2] = 2 * near_clip * far_clip / (near_clip - far_clip);
proj[0][3] = 0.0f;
proj[1][3] = 0.0f;
proj[2][3] = 1.0f;
proj[3][3] = 0.0f;

fx,fy是焦距,宽度和高度是图像的宽度和高度,cx和cy是宽度/ 2和高度/ 2(通过它可以得到相机矩阵)。

然后我将4 * 4外在矩阵换为glm视图矩阵,这是因为opengl中的列主要。在我的着色器中,通过

获得了gl_Position
gl_Position = proj * view * vec4(3Dpointposition, 1.0f)

但是我在屏幕上得到了错误的渲染结果。我的相机矩阵的原点在左上角,而opengl的原点在左下角,似乎opencv中的相机向下看正Z轴,但opengl相机向下看负数z -轴。如何将代码修改为正确的版本?

0 个答案:

没有答案
相关问题