OpenGL Center of rotation?
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!
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!
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.
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.
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!
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!
Read the gluLookAt manpage.
http://answers.google.com/answers/thread...61441.html
(skip down to "summary", halfway down the page, if you like)
(skip down to "summary", halfway down the page, if you like)
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
New game in development Rubber Ninjas  Mac Games Downloads
What you need to understand specifically is the difference between world and local matrices. Apply your translation and rotation to the correct matrix.
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 coordinate 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);
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 coordinate 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);
Possibly Related Threads...
Thread:  Author  Replies:  Views:  Last Post  
OpenGl rotation/translation problems?  wyrmmage  2  6,856 
Jan 6, 2008 01:58 PM Last Post: wyrmmage 

Getting Current Translation And Rotation In OpenGL  Nick  3  5,435 
Jul 26, 2006 11:16 AM Last Post: kelvin 

2D OpenGL With SDL Rotation + Scaling Problem  Greywhind  4  6,141 
Jun 2, 2006 04:16 PM Last Post: Greywhind 

Quaternion rotation > OpenGL rotation matrix?  DJBlufire  3  5,722 
Sep 6, 2003 08:34 PM Last Post: MattDiamond 