2013-03-22 137 views
3

我正在使用android中的增强现实应用程序。目前我对Open GL ES 2.0有些问题.. 首先,我想旋转一个对象,然后翻译它,但它不起作用。单个转换,意味着只有旋转或翻译,正在工作。 我认为我的问题与OpenGL - Translate after rotate类似,但那里的答案对我没有帮助。 例如,如何将对象围绕z轴旋转45度,然后将它向左或向右平移100px?OpenGL ES 2旋转后的翻译

下面是一些代码:

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

UDATE: 我发现有时欧拉角一定是错误的或我的不正确的方式使用它们。我不知道。当我使用Matrix.rotateM(mModelMatrix, 0, 90, 0, 0, 1);AFTER翻译一切正常。 现在我不知道如何使用我的欧拉角度与rotateM()。我曾试着拨打了Methode 3次,但后来我得到一个错误的方向转动:

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); 
+0

“不工作”以什么方式?你预期的和实际的结果是什么? – Michael 2013-03-22 09:51:02

+0

当在z轴上将对象旋转45度,然后在x轴上将其翻译为100px时,我期望...但实际上该对象在45度旋转的x轴上平移。换句话说,我想独立翻译它,但我不知道如何。也许我可以添加一些截图到我的问题 – Shataya 2013-03-22 10:01:32

+1

如果你交换翻译和旋转? – JasonD 2013-03-22 10:03:27

回答

0

我想我已经找到了有效的解决方案。函数“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); 
+0

只需要更多的信息:欧拉角会导致一个称为“万向节锁定”问题的数学问题。所以,只有2个轴可以同时旋转。像你一样旋转3是最好的方法。 – 2017-05-12 12:51:07