使用矩阵.在OpenGL ES 2.0中旋转

Zip*_*ppy 7 android rotation opengl-es-2.0 translate-animation

编辑 - 添加了更多代码

尝试使用OpenGL ES 2.0正确旋转我的四边形时遇到很多问题.

它似乎总是围绕屏幕坐标的中心旋转.我试图让它围绕它自己的中心旋转(2d,所以只有z轴).

我一直在尝试使用Matrix.translate,如下所示.但是,在此处更改x或y pos只是将四边形绘制在屏幕上的不同位置,但是当它旋转时,它再次围绕屏幕的中心旋转.请有人解释如何让它围绕它自己的z轴旋转(就像一个轮子)?

谢谢,这里是相关的代码行 - 如果需要更多,请询问,我会发布.(请注意,我在SO和更广泛的互联网上看了很多类似的问题,但到目前为止我还没有找到答案).

谢谢.

//Set rotation
Matrix.setRotateM(mRotationMatrix, 0, -angle, 0, 0, 1.0f);

//Testing translation
Matrix.translateM(mRotationMatrix, 0, -.5f, .5f, 0f);

// Combine the rotation matrix with the projection and camera view
Matrix.multiplyMM(mvpMatrix, 0, mRotationMatrix, 0, mvpMatrix, 0);
Run Code Online (Sandbox Code Playgroud)

我的着色器(在班级宣布)

private final String vertexShaderCode =
"uniform mat4 uMVPMatrix;" +

"attribute vec4 vPosition;" +
"void main() {" +
" gl_Position = uMVPMatrix * vPosition;" +
"}";

private final String fragmentShaderCode =
"precision mediump float;" +
"uniform vec4 vColor;" +
"void main() {" +
" gl_FragColor = vColor;" +
"}";
Run Code Online (Sandbox Code Playgroud)

从onSurfaceChanged

float ratio = (float) width / height;
Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
Run Code Online (Sandbox Code Playgroud)

在我的onDrawFrame方法中

// 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);
Run Code Online (Sandbox Code Playgroud)

esc*_*tor 5

我遇到了同样的问题(看到奇怪的扭曲和其他一切),我的解决方案基于Android培训>使用OpenGL ES显示图形>添加动作如下.

(如果需要,请转到我的详细帖子: OpenGL ES Android Matrix Transformations.)

  1. mModelMatrix设置为标识Matrix

    Matrix.setIdentityM(mModelMatrix, 0); // initialize to identity matrix
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将翻译应用于mModelMatrix

    Matrix.translateM(mModelMatrix, 0, -0.5f, 0, 0); // translation to the left
    
    Run Code Online (Sandbox Code Playgroud)
  3. 将旋转应用于mRotationMatrix(以度为单位的角度)

    Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);
    
    Run Code Online (Sandbox Code Playgroud)
  4. 通过Matrix.multiplyMM组合旋转和平移

    mTempMatrix = mModelMatrix.clone();
    Matrix.multiplyMM(mModelMatrix, 0, mTempMatrix, 0, mRotationMatrix, 0);
    
    Run Code Online (Sandbox Code Playgroud)
  5. 将模型矩阵与投影和摄像机视图相结合

    mTempMatrix = mMVPMatrix.clone();
    Matrix.multiplyMM(mMVPMatrix, 0, mTempMatrix, 0, mModelMatrix, 0);
    
    Run Code Online (Sandbox Code Playgroud)


joh*_*ers 0

旋转通常围绕原点发生,因此您需要在平移四边形之前旋转它。如果平移后旋转,则四边形将首先远离原点,然后绕原点旋转。

在不知道您的 Matrix 函数是如何实现的情况下,我们无法建议您是否正确使用它们。您在函数界面中向我们展示的所有内容。

但一般来说,在平移之前先旋转。