## OpenGL Center of rotation?

Nibbie
Posts: 2
Joined: 2008.09
Post: #1
Hello,

I am developing an OpenGL application and I am trying to rotate the camera view centered at an arbitrary point (say anywhere but 0,0,0). Is this possible? Or does the camera only rotate centered on 0,0,0?

I have attempted using glTranslate and glRotate, but no matter what I do, it is rotated at the origin (0,0,0).

It seems like a huge limitation to me if you have to move all of my objects centered on 0,0,0 to rotate at that center. I will also have to do many, many more calculations which will of course adversely affect performance.

Thanks!
Moderator
Posts: 1,559
Joined: 2003.10
Post: #2
It sounds like your mental model of how transformations work is at a significantly higher level than the way OpenGL works. All drawn primitives are transformed by two 4x4 transformation matrices, which means that you can apply any transformation that can be expressed with one to any primitive in your scene (which includes any rotation, translation, scale, shear, foreshortening, and other things).

I'd suggest reading up on how matrices work. Here's a tutorial I wrote that could help get you started: http://www.sacredsoftware.net/tutorials/...ices.xhtml

To more directly reply to your post, yes, what you want to do is possible, and no, it won't adversely affect performance.
Nibbie
Posts: 2
Joined: 2008.09
Post: #3

I have read the 3rd chapter of the Red Book ("Viewing") and the 4th chapter of the "OpenGL SuperBible" (Geometric Transformations: The Pipeline), but I cannot figure out how to do do a rotation centered on any point other than 0,0,0.

It is really frustrating to me, because it seems like such a simple thing to accomplish and I have spent days trying to figure it out. I have a fairly good understanding how the matrices work, but I have no idea to actually rotate centered on some arbitrary point.

Any examples would be Greatly Appreciated!
Sage
Posts: 1,231
Joined: 2002.10
Post: #4
Luminary
Posts: 5,139
Joined: 2002.04
Post: #5

(skip down to "summary", half-way down the page, if you like)
Member
Posts: 749
Joined: 2003.01
Post: #6
The easiest way to do this is using gluLookAt to place the camera, then for each object: glPushMatrix, glTranslatef to place the object and glRotatef to rotate it, then glPopMatrix.

Â©hâ‚¬ck Ã¸ut Âµy stuÆ’Æ’ Ã¥t ragdollsoft.com
Member
Posts: 241
Joined: 2008.07
Post: #7
What you need to understand specifically is the difference between world and local matrices. Apply your translation and rotation to the correct matrix.
Nibbie
Posts: 1
Joined: 2008.09
Post: #8
If you apply a pure rotation matrix, you can see that all points that are mapped onto self lie on a straight line that passes through 0. This line is called the axis of rotation. Due to this, rotations ever perform around zero.

Now, if you want another point as origin, you simply have to translate the world so that exactly the desired point becomes 0. Then rotation can be done. At last the translation is to be "undone", so that the world appears as before. I.e.
T * R * inv(T)
when using column vectors (as usual for OpenGL). Herein T denotes the matrix of translation to the desired origin, and R the rotation matrix.

The above formula fit to the sequence
glTranslatef(x,y,z);
glRotatef(...);
glTranslatef(-x,-y,-z);
of OpenGL routines, where (x,y,z) is the desired origin of rotation.

Now you have to consider that you want to rotate the camera. The view portion of the MODELVIEW matrix is the inverse of the camera's local co-ordinate frame, Hence, if you want to apply the transformations directly, you need
inv( T * R * inv(T) ) == T * inv(R) * inv(T)

In summary, try
glMatrixMode(GL_MODELVIEW);