iDevGames Forums
OpenGL Center of rotation? - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: OpenGL Center of rotation? (/thread-2389.html)



OpenGL Center of rotation? - 3DTOPO - Sep 19, 2008 03:28 PM

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!


OpenGL Center of rotation? - ThemsAllTook - Sep 19, 2008 05:07 PM

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/Matrices/Matrices.xhtml

To more directly reply to your post, yes, what you want to do is possible, and no, it won't adversely affect performance.


OpenGL Center of rotation? - 3DTOPO - Sep 19, 2008 05:21 PM

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.

Wacko

Any examples would be Greatly Appreciated!


OpenGL Center of rotation? - arekkusu - Sep 19, 2008 05:40 PM

Read the gluLookAt manpage.


OpenGL Center of rotation? - OneSadCookie - Sep 19, 2008 06:22 PM

http://answers.google.com/answers/threadview/id/361441.html

(skip down to "summary", half-way down the page, if you like)


OpenGL Center of rotation? - Najdorf - Sep 19, 2008 07:06 PM

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.


OpenGL Center of rotation? - bmantzey - Sep 20, 2008 08:09 PM

What you need to understand specifically is the difference between world and local matrices. Apply your translation and rotation to the correct matrix.


OpenGL Center of rotation? - haegarr - Sep 26, 2008 01:30 PM

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);
glLoadIdentity();
glTranslatef(x,y,z);
glRotatef(...); // with the inverse of the wanted rotation
glTranslatef(-x,-y,-z);