3

I'm working on a augmented reality app in android. At the moment I have some problems with the Open GL ES 2.0 stuff.. First I want to rotate an object and then translate it but it doesn't work.. Single transformations, means only rotation or translation, are working. I think my question is similar to OpenGL - Translate after rotate but the answers there don't help me. For example how can I rotate the object by 45 degrees about the z-axis and then translate it 100px left or right?

Here is some code:

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: I found out that sometimes the euler angles must be wrong or I use them in a incorrect way. I don't know. When I use Matrix.rotateM(mModelMatrix, 0, 90, 0, 0, 1); AFTER translation everything works fine. Now I don't know how I can use my euler angles with rotateM(). I have tried to call the methode three times but then I get a wrong rotation:

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);
Community
  • 1
  • 1
Shataya
  • 144
  • 1
  • 10
  • "doesn't work" in what way? What's your expected and actual results? – Michael Mar 22 '13 at 09:51
  • When Rotating the object by 45 degrees on the z-axis and then translating it 100px on the x-axis, I expect that... but actual the object is translated on a 45 rotated x-axis. In other words I want to translate it independently but I don't know how. Maybe I can add some screenshots to my question – Shataya Mar 22 '13 at 10:01
  • 1
    And if you swap the translation and rotation around? – JasonD Mar 22 '13 at 10:03
  • 1
    @user2198435: Have you considered doing the operations in the reverse order? – Nicol Bolas Mar 22 '13 at 10:14
  • Then the rotation center doesn't fit. Hm.. I can't post any images because of my number of reputations. Here is a [link](http://i.imagebanana.com/img/423b3fd7/screenshot.jpg) to the image. I want the rotated square to be at the position of the other square. Both have actually the same translation.. (Edit: I mean the squares on the right side) – Shataya Mar 22 '13 at 10:16
  • Ensure you are rotating around the *middle* of the object. You might need to translate to the middle first (the pivot point). – Bartek Banachewicz Mar 22 '13 at 10:18
  • I'm really confused now because I have made some tests again and I don't see any differences when I swap it. And sometimes it's working.. – Shataya Mar 22 '13 at 12:37
  • IOW you are doing something wrong. – Bartek Banachewicz Mar 22 '13 at 13:23
  • Could someone specify "something"? I really have no idea anymore how I can use these euler angles. I thought that the code I've posted was the solution but it works not in every case with the translation. Just rotating works always. – Shataya Mar 22 '13 at 14:38
  • "When Rotating ... but actual the object is translated on a 45 rotated x-axis. " That's because the matrix transforms don't move the object - they transform the coordinate system. To start, the origin is in the upper left corner, the x axis points right, and the y axis points down. After a 45 deg rotation, the origin is still in the upper right corner, the x-axis and y-axis are now rotated 45 deg. So now when you do a translation along the "x-axis", that is a translation along a 45 degree angle on the screen. – mbeckish Mar 22 '13 at 15:22

1 Answers1

0

I think that I have found a working solution. The function "setRotateEulerM()" seems to make some problems in my code before.. Now I make single rotations with "rotateM()". It's important to set the z-coordinate to -1. Here is my code:

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);
Shataya
  • 144
  • 1
  • 10
  • Just a bit more information: Euler angles causes a mathematical problem called Gimbal Lock problem. So, only 2 axis can rotate simultaneously. Making 3 rotateM like you did is the best way. – Jordan Junior May 12 '17 at 12:51