1

I am trying to translate an object in world space after rotating it at the origin. The problem is, after I rotate an object while it's at the origin, its local coordinate system rotates as well, so its axes are not parallel with the world coordinate axes anymore. So, for example, if I needed to translate an object to the point (3, 0, 5) after rotating the object around the y-axis, I would not be able to do this since the object's local coordinate system isn't the same as the world coordinate system. For example:

glRotatef(45, 0.0, 1.0, 0.0);
glTranslatef(3.0, 0.0, 5.0);
glutSolidCube(1);               // This won't be at (3,0,5) like how I need it

How can I overcome this?

Drake
  • 2,679
  • 4
  • 45
  • 88

2 Answers2

2

This is not so much a property of OpenGL, but matrix math.

When you send in a vertex to the OpenGL fixed function pipeline, it gets transformed by the matrices modelview and projection, i.e.

 v_eye = MODELVIEW · v
v_clip = PROJECTION · v_eye

Now the modelview matrix is composed of all those sub-transformations. And they're non commutative, i.e. order of operations matters. For example

MODELVIEW = T_0 · R_0 · R_1 · T_1 · S_1 · R_2

Now you can rewrite the v_eye calculation above as

v_eye = T_0 · ( R_0 · ( R_1 · (T_1 · (S_1 · (R_2 · v)))))

As you can see, the v is multiplied (just like the matrices) right to left onto the stack of matrices you composed modelview of.

However what happens in OpenGL is, that the one matrix "MODELVIEW" is calculated, and v is transformed only with that 4×4 scheme of numbers. OpenGL does not "re-execute" all the matrix multiplication steps.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • Thanks for your response. But I still don't see how I can freely translate an object to a specific coordinate after it has been rotated. – Drake Oct 30 '12 at 19:10
  • @user1136506: First rotate the object, then translate it. And since the matrices are right associative the translates to a call to glRotate before doing glTranslate. – datenwolf Oct 30 '12 at 19:33
  • I got it to work. I had the wrong idea but that cleared things up a lot, thanks! – Drake Nov 01 '12 at 07:28
-1

opengl statement run on GPU in reverse order.

So Translate first then rotate.

Techie Manoj
  • 432
  • 3
  • 14
  • 1
    OpenGL statements do not directly translate into GPU operations. In fact there were only one "GPU" in history that sort-of did; and OpenGL matrix operations never were executed on that one either. The way matrices behave is, because they're matrices. Also they don't "run" in inverse order. – datenwolf Oct 30 '12 at 07:53