OpenGL ES 2.0:从正交到透视(卡片翻转效果)

时间:2017-11-04 17:38:31

标签: java android opengl-es opengl-es-2.0 coordinate-transformation

我以这种方式设置正交矩阵:

ratio = (float) width / height;
left = -ratio;
right = ratio;
bottom = -1f;
top = 1f;
Matrix.orthoM(projectionMatrix, 0, left, right, bottom, top, -1f, 1f);
viewProjMatrix = projectionMatrix;

以这种方式创建一些图块:

float widthF = (right - left) / numColumns; // left to right
float heightF = (bottom - top) / numRows; // top to bottom
for (int row = 0; row < numRows; ++row) {
    for (int col = 0; col < numColumns; ++col) {
        float x1 = left + col * widthF;
        float y1 = top + row * heightF;
        float x2 = left + (col + 1) * widthF;
        float y2 = top + (row + 1) * heightF;
        float z = 0f;
        // and so on, add to vertex buffer
    }
}

结果是:

orthogonal projection

如果我使用透视矩阵而不是正交矩阵,这看起来会更好:

Matrix.frustumM(projectionMatrix, 0, left, right, bottom, top, 1f, 10f);
Matrix.setLookAtM(viewMatrix, 0, 0f, 0f, 2f, 0f, 0f, 0f, 0f, 1f, 0f);
Matrix.multiplyMM(viewProjMatrix, 0, projectionMatrix, 0, viewMatrix, 0);

结果是:

perspective projection

但是,如何让图像以“全屏”显示,这意味着填充视口?正交矩阵填充视口的方式,但这次是透视。

(此卡翻转效果受到Flux Slider的Tiles3D过渡的启发

1 个答案:

答案 0 :(得分:1)

投影矩阵描述了从场景的3D点到视口的2D点的映射。投影矩阵从视图空间转换到剪辑空间,剪辑空间中的坐标转换为范围(-1,-1,-1)到(1,1,1)范围内的规范化设备坐标(NDC)通过使用剪辑坐标的w分量进行划分。

在Perspective Projection中,投影矩阵描述了从针孔相机到视口的2D点看世界中3D点的映射。
相机平截头体(截头金字塔)中的眼睛空间坐标被映射到立方体(标准化设备坐标)。

enter image description here

视图空间中的投影区域与视图空间的Z坐标之间的实现是线性的。它取决于视角和纵横比。

enter image description here

您必须将fiew的字段调整为图像。为此,您必须知道图像与相机位置的距离。

float distZ = ....; // distance of the camera to the image
float sizeX = ....; // width of the rectangle where the image is placed in
float sizeY = ....; // height of the rectangle where the image is placed in

float near = 1f;
float far  = 10f;

float left   = -0.5 * sizeX * near / distZ;
float right  =  0.5 * sizeX * near / distZ;
float bottom = -0.5 * sizeY * near / distZ;
float top    =  0.5 * sizeY * near / distZ;

Matrix.frustumM(projectionMatrix, 0, left, right, bottom, top, near, far);

注意,当然图像必须是#34;居中&#34;观点。