android - 旋转后的 OpenGL ES 2 翻译

标签 android opengl-es matrix rotation translation

我正在 Android 中开发增强现实应用程序。目前我在使用 Open GL ES 2.0 时遇到了一些问题...... 首先,我想旋转一个对象,然后对其进行平移,但它不起作用。单一变换,意味着只有旋转或平移,才有效。 我想我的问题类似于 OpenGL - Translate after rotate但那里的答案对我没有帮助。 例如,如何将对象绕 z 轴旋转 45 度,然后将其向左或向右平移 100 像素?

这是一些代码:

Matrix.setIdentityM(mModelMatrix, 0);
// Euler angles in res[] from opencv 
Matrix.setRotateEulerM(mRotateMatrix, 0,  (float) res[0], (float) res[1], (float) res[2]);
Matrix.multiplyMM(mModelMatrix, 0, mRotateMatrix , 0, mModelMatrix, 0);
Matrix.translateM(mModelMatrix, 0, (x-hmWidth),(y-hmHeight)*(-1), 0);
Matrix.scaleM(mModelMatrix, 0, (float) arc.getSize().width, (float) arc.getSize().height, 10);
drawBlock(gl);

更新: 我发现有时欧拉角一定是错误的,或者我以不正确的方式使用它们。我不知道。当我使用 Matrix.rotateM(mModelMatrix, 0, 90, 0, 0, 1); AFTER 翻译时,一切正常。 现在我不知道如何将我的欧拉角与 rotateM() 一起使用。我已经尝试调用该方法三次,但是我得到了错误的轮换:

Matrix.rotateM(mModelMatrix, 0, (float) res[0], 1, 0, 0);
Matrix.rotateM(mModelMatrix, 0, (float) res[1], 0, 1, 0);
Matrix.rotateM(mModelMatrix, 0, (float) res[2], 0, 0, 1);

最佳答案

我想我已经找到了可行的解决方案。函数“setRotateEulerM()”之前似乎在我的代码中出现了一些问题。现在我使用“rotateM()”进行单次旋转。将 z 坐标设置为 -1 很重要。 这是我的代码:

Matrix.setIdentityM(mModelMatrix, 0);
res = arc.getEulerAngles();

x = (int) ( (float) arc.getCenter().x / diffx);
y = (int) ( (float) arc.getCenter().y / diffy);
Matrix.translateM(mModelMatrix, 0, (x-hmWidth),(y-hmHeight)*(-1), -10f);
Matrix.rotateM(mModelMatrix, 0, (float) res[0], 1, 0, 0);
Matrix.rotateM(mModelMatrix, 0, (float) res[1], 0, 1, 0);
Matrix.rotateM(mModelMatrix, 0, (float) res[2], 0, 0, -1);

Matrix.scaleM(mModelMatrix, 0, (float) arc.getSize().width, (float) arc.getSize().height, 1);
drawBlock(gl);

关于android - 旋转后的 OpenGL ES 2 翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15566851/

相关文章:

android - 从相机预览中丢帧

android - 如何在 ListView 中显示已安装在 Android 手机上的应用程序列表?

java - 在两个不同的 vbo 之间进行插值时,网格面会迷失方向

ios - 如何准确放大捏合手势的特定点(多次捏合)?

R矩阵获得两个最高值的索引

java - 安卓自定义进度条

android - Gtalk XMPP SASL 身份验证使用机制 X-OAUTH2 失败?

android - 如何在 android opengl 1.0 应用程序的最大纹理宽度上过滤手机

android - OpenGL ES 3.0 遮挡查询始终结果为 false

matlab - 在 MATLAB 中找到矩阵的最大公约数