官术网_书友最值得收藏!

Repositioning the triangle

Our matrix-fu has really gotten us places. Let's go further.

I want to move the triangle out of the way. We'll do this by setting up another transformation matrix and then using it on the model when it's time to draw.

Add two new matrices named triTransform and triView:

    // Model variables
    private float[] triTransform;

    // Viewing variables
    private float[] triView;

Initialize them in onCreate as well:

        triTransform = new float[16];
        triView = new float[16];

Let's set the model matrix that positions the triangle in the initializeScene method (called by onSurfaceCreated). We'll offset it by 5 units in X and backwards 5 units in Z. Add the following code to initializeScene:

       // Position the triangle
        Matrix.setIdentityM(triTransform, 0);
        Matrix.translateM(triTransform, 0, 5, 0, -5);

Lastly, we use the model matrix to build the modelViewProjection matrix in onDrawEye. Modify onDrawEye, as follows:

    public void onDrawEye(Eye eye) {
        ...
        // Apply perspective transformation to the view, and draw
        Matrix.multiplyMM(triView, 0, view, 0, triTransform, 0);
        Matrix.multiplyMM(modelViewProjection, 0, perspective, 0, triView, 0);
        drawTriangle();
    }

Build and run it. You will now see the triangle further away and off to the side.

Note

To summarize one more time: the modelViewProjection matrix is a combination of the triangle's position transform (triTransform), the camera's location and orientation (camera), the current eye's viewpoint from CardboardView based on the phone's motion sensors (eye.getEyeView), and the perspective projection. This MVP matrix is handed to the vertex shader to determine its actual location when drawing the triangle on the screen.

主站蜘蛛池模板: 赤峰市| 广宁县| 斗六市| 沂南县| 通许县| 五指山市| 开封县| 海盐县| 诸暨市| 元阳县| 永泰县| 乌拉特后旗| 延寿县| 师宗县| 南岸区| 霍邱县| 韩城市| 杨浦区| 临洮县| 绍兴县| 光泽县| 永济市| 扬州市| 东莞市| 大城县| 怀远县| 循化| 西峡县| 分宜县| 黄梅县| 曲麻莱县| 赣州市| 古田县| 彭泽县| 舟曲县| 驻马店市| 宿迁市| 扎赉特旗| 乌兰浩特市| 孟州市| 闽清县|