0

我正在使用默认的 android 示例代码 http://developer.android.com/training/graphics/opengl/touch.html 在此示例中,我们可以通过 toucht 事件旋转三角形。

我只想添加 x,y 轴的运动以进行测试。三角形行为并不像我预期的那样。我做错了什么?

教程中的代码突出显示了我的新行:

public void onDrawFrame(GL10 unused) {

        // Draw background color
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

        // Set the camera position (View matrix)
        Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

        // Calculate the projection and view transformation
        Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);


        // Draw square
        mSquare.draw(mMVPMatrix);



       **//Translating this matrix 'brakes' triangle
  ->      Matrix.translateM(mMVPMatrix, 0, 0, pos, -1.0f);

        //NOTHING happens here: ??? Why?
  ->      Matrix.translateM(mRotationMatrix, 0, pos, 0, -1.0f);**

        // Create a rotation for the triangle
        // long time = SystemClock.uptimeMillis() % 4000L;
        // float angle = 0.090f * ((int) time);
        Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);

        // Combine the rotation matrix with the projection and camera view
        Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0);

        // Draw triangle
        mTriangle.draw(mMVPMatrix);
    }

默认行为:

在此处输入图像描述

用我的代码:

在此处输入图像描述

4

1 回答 1

1

感谢icrev评论:

您无法对 MVP 矩阵进行平移/旋转/缩放并获得预期的结果。

您必须在模型矩阵中平移/旋转对象(或在视图矩阵中进行相机平移/旋转)。

看看这个模型视图投影矩阵的目的, 以更好地理解你需要做什么

这些是步骤:

  1. 将 M 矩阵设置为单位矩阵。平移或旋转它。注意万向节锁 (en.wikipedia.org/wiki/Gimbal_lock)

  2. 设置 V 矩阵 Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f); 3.您已经有投影矩阵(在您的情况下为 mProjMatrix)

    1. 乘 M * V * P 得到最终的 MVP 矩阵
于 2013-04-28T16:23:51.887 回答