使用矩阵.在OpenGL ES 2.0中旋转 [英] Using Matrix. Rotate in OpenGL ES 2.0

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

问题描述

编辑-添加了更多代码

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

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

它总是围绕屏幕坐标中心旋转.我正在尝试使其绕其自身的中心旋转(对于2d,仅适用于z轴).

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).

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

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)?

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

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).

谢谢.

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

我的着色器(在课堂上宣布)

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

来自onSurfaceChanged

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

在我的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);

推荐答案

我遇到了相同的问题(见过怪异的变形和其他所有问题),我的解决方案基于

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.

(如果需要,请转到我的详细信息,网址为: OpenGL ES Android矩阵转换.)

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

  1. 设置 mModelMatrix 以标识Matrix

  1. Set a mModelMatrix to identity Matrix

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

  • 将翻译应用于 mModelMatrix

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

  • 将旋转应用于 mRotationMatrix (角度)

  • Apply rotation to a mRotationMatrix (angles in degrees)

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

  • 通过 Matrix.multiplyMM

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

  • 将模型矩阵与投影和相机视图合并在一起

  • 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中旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆