本文介绍了在OpenGL ES 2.0的使用Matrix.Translate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改 - 增加了更多的code

有使用OpenGL ES 2.0的很多尝试正常旋转我四问题。

有似乎总是在屏幕坐标的中心旋转。我试图让它围绕它自己的中心旋转(二维,只有这么Z轴)。

我一直在尝试Matrix.translate如下秀。然而,在这里改变了x或y POS简单地绘制四在屏幕上的不同位置,但是,当它旋转时,它再次围绕屏幕中心旋转。请可能有人解释如何得到它围绕旋转它自己的Z轴(像一个车轮)?

谢谢,这里有code的相关线路 - 如果需要更多,请咨询我将发布。 (请注意,我已经看了很多关于这样类似的问题和更广泛的互联网,但我没有设法找到一个答案到目前为止)。

感谢。

  //设置旋转
Matrix.setRotateM(mRotationMatrix,0,利用广角,0,0,1.0F);//翻译测试
Matrix.translateM(mRotationMatrix,0,-.5f,.5f,0F);//合并旋转矩阵与投影和相机视图
Matrix.multiplyMM(mvpMatrix,0,mRotationMatrix,0,mvpMatrix,0);

我的着色器(在类级别声明)

 私有最终字符串vertexShader code =
统一mat4 uMVPMatrix; +属性vec4 vPosition; +
无效的主要(){+
GL_POSITION = uMVPMatrix * vPosition; +
};私人最终字符串fragmentShader code =
precision mediump浮动; +
统一vec4 vColor; +
无效的主要(){+
gl_FragColor = vColor; +
};

从onSurfaceChanged

 浮动率=(浮点)宽/高;
Matrix.frustumM(mProjMatrix,0,-ratio,比率,-1,1,3,7);

在我onDrawFrame方法

  //设置相机位置(视图矩阵)
Matrix.setLookAtM(mVMatrix,0,0,0,3,0F,0F,0F,0F,1.0F,0.0);//计算投影和视图变换
Matrix.multiplyMM(mMVPMatrix,0,mProjMatrix,0,mVMatrix,0);


解决方案

从来就遇到了同样的问题(看到怪异的扭曲和其他一切),我的解决方案基础上的。)


  1. 设置的 mModelMatrix 的身份矩阵

      Matrix.setIdentityM(mModelMatrix,0); //初始化为单位矩阵


  2. 应用转换到的 mModelMatrix

      Matrix.translateM(mModelMatrix,0,-0.5f,0,0); //翻译向左


  3. 应用旋转到的 mRotationMatrix 的(度角)

      Matrix.setRotateM(mRotationMatrix,0,裂伤,0,0,-1.0F);


  4. 通过联合旋转和平移的 Matrix.multiplyMM

      mTempMatrix = mModelMatrix.clone();
    Matrix.multiplyMM(mModelMatrix,0,mTempMatrix,0,mRotationMatrix,0);


  5. 联合模型矩阵与投影和相机视图

      mTempMatrix = mMVPMatrix.clone();
    Matrix.multiplyMM(mMVPMatrix,0,mTempMatrix,0,mModelMatrix,0);


Edit - Added more code

Having a lot of problems attempting to correctly rotate my quad using OpenGL ES 2.0.

It always seems to rotate around the centre of the screen co-ordinates. I'm trying to get it to rotate around it's own centre (for 2d, so z axis only).

I've been experimenting with Matrix.translate as show below. However, changing the x or y pos here simply draws the quad at a different place on the screen, but when it rotates, again it rotates around the centre of the screen. Please could someone explain how to get it to spin around it's own z axis (like a wheel)?

Thanks, here are the relevant lines of code - if more is needed, please ask and I will post. (Please note, I've looked at a lot of similar questions on SO and the wider internet but I've not managed to find an answer thus far).

Thanks.

//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);

My Shaders (declared at class level)

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;" +
"}";

From onSurfaceChanged

float ratio = (float) width / height;
Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);

In My onDrawFrame method

// 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);
解决方案

I´ve encountered the same problems (seen weird distortions and everything else), my solution based on the Android Training > Displaying Graphics with OpenGL ES > Adding Motion below.

(Head over to my detailed post for at if needed:OpenGL ES Android Matrix Transformations.)

  1. Set a mModelMatrix to identity Matrix

    Matrix.setIdentityM(mModelMatrix, 0); // initialize to identity matrix
    

  2. Apply translation to the mModelMatrix

    Matrix.translateM(mModelMatrix, 0, -0.5f, 0, 0); // translation to the left
    

  3. Apply rotation to a mRotationMatrix (angles in degrees)

    Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);
    

  4. Combine rotation and translation via Matrix.multiplyMM

    mTempMatrix = mModelMatrix.clone();
    Matrix.multiplyMM(mModelMatrix, 0, mTempMatrix, 0, mRotationMatrix, 0);
    

  5. Combine the model matrix with the projection and camera view

    mTempMatrix = mMVPMatrix.clone();
    Matrix.multiplyMM(mMVPMatrix, 0, mTempMatrix, 0, mModelMatrix, 0);
    

这篇关于在OpenGL ES 2.0的使用Matrix.Translate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 15:40