OpenGL Center of rotation?

Posts: 2
Joined: 2008.09
Post: #1

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.

Quote this message in a reply
Posts: 1,563
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:

To more directly reply to your post, yes, what you want to do is possible, and no, it won't adversely affect performance.
Quote this message in a reply
Posts: 2
Joined: 2008.09
Post: #3
Thanks for the link.

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!
Quote this message in a reply
Posts: 1,234
Joined: 2002.10
Post: #4
Read the gluLookAt manpage.
Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #5

(skip down to "summary", half-way down the page, if you like)
Quote this message in a reply
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
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
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.
Quote this message in a reply
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
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
glRotatef(...); // with the inverse of the wanted rotation
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGl rotation/translation problems? wyrmmage 2 6,973 Jan 6, 2008 01:58 PM
Last Post: wyrmmage
  Getting Current Translation And Rotation In OpenGL Nick 3 5,545 Jul 26, 2006 11:16 AM
Last Post: kelvin
  2D OpenGL With SDL Rotation + Scaling Problem Greywhind 4 6,276 Jun 2, 2006 04:16 PM
Last Post: Greywhind
  Quaternion rotation -> OpenGL rotation matrix? DJBlufire 3 5,831 Sep 6, 2003 08:34 PM
Last Post: MattDiamond