   Rotating an object in the world coordinate
  Hi, this is my first post(WITH LOTS OF HOPE)I have the same problem as this Page here .In my case there are multiple object in a scene and a particular object have to rotate while other object should not rotate.There is a solution given at the bottom I tried to follow it. My logic is saying that it is the correct one but my program is not giving the same output.    glPushMatrix();   // No identity is loaded becaue there are many objects, but is called before all the objects are drawnglTranslatef(pos.x, pos.y, pos.z);   //////////////////////////////////// The code I am Concerned withGLfloat m[16];glGetFloatv(GL_MODELVIEW_MATRIX, m);glLoadIdentity();glRotatef(dx, 1.0f, 0.0f, 0.0f);glRotatef(dy, 0.0f, 1.0f, 0.0f);dx = dy = 0.0f;glMultMatrixf(m);////////////////////////////////////glScalef(scale.x, scale.y, scale.z);// Actual drawing codeIf this particular code is ran then my object just shakes with the change of dx and dy. The dx and dy are float values generated due to user interaction and they are different every time and is never more than 5.0f.If I comment out dx = dy = 0 then the object rotate all around the world coordinate space.Please help me out.   
  Can you please explain your intention in more detail?The code snippet in the OP is legal but very uncommon. And what happens before the shown topmost glPushMatrix?Usually, one does something like the following:glLoadIdentity();// multiply here with VIEW matrix, e.g. by using gluLookAt or the inverse of a camera frameforeach( object ) {    glPushMatrix();    // multiply here with the local frame of object    // draw here the object    glPopMatrix():}Is this suitable for your needs?   ;Thanks for a very prompt reply.Please see this linkhttp://www.bit-101.com/blog/?p=1835I am developing for iPhone.Each object (loaded from obj files) are separate objects. each having own rendering functions.After initializing the objects, all the rendering calls are issued from a CENTRAL RENDERING function.It is a pretty big code so I am using a bits of it.CENTRAL RENDERING FUNCTION(){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glPushMatrix();glLoadIdentity(); glColor4f(0.0, 0.5, 1.0, 1.0);gluLookAt(0.5f, 7.5f, 14.0f, 0.0f, 3.0f, 0.0f, 0.0f, 1.0f ,0.0f); //Here goes my draw code.   [object1 render];   [object2 render];   ..;   //Here are 10 objects}The code in the render part of each object is posted in THE FIRST POST.Now I think you have a fair Idea.Basically:-1. I want to save the current modelview matrix which contains the data of the current rotation,2. Reset it.3. Apply the new rotation in the reset order.4. Apply the previous rotation by multiplying the current matrix by previously saved model view matrix.The Reason : For a finger swipe in a particular direction I want to rotate the object With Respect to the current ORIENTATION OF THAT PARTICULAR OBJECT.Am I doing anything wrong. I have been banging my head to this for about a week.;  Lets say that the 3d object have a orientation in the 3d axes. When the user swipe in the screen to rotate the object, The object should rotate with respect to the current orientation.   ;  i dont see the point of the following code GLfloat m[16];glGetFloatv(GL_MODELVIEW_MATRIX, m);glLoadIdentity();glRotatef(dx, 1.0f, 0.0f, 0.0f);glRotatef(dy, 0.0f, 1.0f, 0.0f);dx = dy = 0.0f;glMultMatrixf(m);but then again i dont know how the rest of the program works.anyway instead of using all of that just tryglPushMatrix();  glTranslatef(pos.x, pos.y, pos.z);  glRotatef(dx, 1.0f, 0.0f, 0.0f);  glRotatef(dy, 0.0f, 1.0f, 0.0f);  glScalef(scale.x, scale.y, scale.z);  //Actual Drawing CodeglPopMatrix();for every object you need to drawdont set dx and dy back to 0 instead just increment it with the amount you want to rotate. So if dx was 40 in the current frame and u +1 it the object would rotate by 1 degree between the current frame and the next frame (40 degrees to 41 degrees)Hope that helps.   ;  Thanks eternity for your fast reply.The part you suggested, was exactly what I was doing previously. But now I have the same problem as this link^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^http://www.bit-101.com/blog/?p=1835=====================================================================So now i am trying now to rotate the object with respect to its current(that is the previous orientation ) orientation.PLEASE SEE THE LINK BEFORE POSTING. That is the part I am having trouble with :(   ;  sorry for not reading through it all the first time.your problem is whats called a Gimbal Lock. Its something you encounter when using Eular angles. the way to get around it would be to use Quaternions instead to represent your rotations. Other than that the rest of the program is fine.so youl have to do something like this:convert Eular Angle (dx,dy,0) to Quaternion Aconvert quaternion A to 4x4 Matrix BglMultMatrix(B);ORconvert Axis Angle(dx,1,0,0) to Quarternion Aconvert Axis Angle(dy,0,1,0) to Quarternion Bmultiply A and B to give Cconvert quaternion C to 4x4 Matrix DglMultMatrix(D);not sure how i did it dont have my code with me :P best to read up on it but one of these two example should eliminate Gimbal LockQuaternions can be a bit tricky i know it took me a good 2 weeks of spare time to get all the math right (googled a lot of articles and tutorials) but its the only way realy.   ;Quote:Original post by sapdevPLEASE SEE THE LINK BEFORE POSTING. That is the part I am having trouble with :(That is clear, but it seems me that the cited thread has other conditions than your problem has. So you should not insist of using that solution.Look at what the cited thread does: It seems to never again glLoadIdentity the MODELVIEW matrix after an initial setup. That hints at that an incremental rotation is applied. Analysing their code, it seems me that the MODELVIEW matrix is computed asRn * ... * R1 * Mwhere Ri denotes the rotation at step i, and M the initial set-up of the MODELVIEW matrix. Usually the set-up means that M is the VIEW matrix V.Further, the cited thread speaks of "doing the rotation in world space". That is true if and only if V is set to the identity matrix, because then the view space and the world space are the same. Otherwise, the rotations are performed in the view space, of course.The above shows that such a way would work only for a single object and no view space change!Analysing your code now, for each object you doRi * V * T * Swhere V!=I, T denotes the translation, and S the scaling. I don't see any glPopMatrix. If you don't have one, then a stack overflow will occur. If you have one behind the rendering code, then you don't accumulate the rotations (hence I've written a single rotation above).Do you see the 3 differences, one of them fundamental to your problem?Coming now to a solution (hopefully I understand your desire correctly): You have to store the individual rotation state for each object. Next, since you want to rotate w.r.t. the view, you have to fetch the view's orientation. That can be done using glGetFloatv just after V has been set-up, and by isolating the right and top vectors from the orientation part (ask if you need help on this, please). The use those in the glRotate invocation instead of the (1,0,0) and (0,1,0) vectors. Further, use the standard order of MODELVIEW set-up as has been shown several times in this thread.Other solutions may exist as well. However, as already said, there are some assumptions I made that may be incorrect. In such a case you may ignore my post or even better correct me, please.[Edited by - haegarr on May 6, 2009 6:45:55 AM];Quote:Next, since you want to rotate w.r.t. the view, you have to fetch the view's orientation. That can be done using glGetFloatv just after V has been set-up, and by isolating the right and top vectors from the orientation part (ask if you need help on this, please)I dont get it :D to be more specific the isolation of right and top vectorsBut one thing is sure you got my problem 110 % right.I have glPopMatrix at the the end. Thanks for pointing out :)And I am really new in this business :( so I am getting lost a bit. What exactly the view matrix means.What I gave get that after applying all transformation the model view matrix is called the view matrix in a general form.Any code snippets will be REALLY HELPFUL.PS: What is the standard way of setting up the model view matrix any link will be again very helpful.And to ETERNITY How can i use quaternion ? i got a library ready from somewhere internet back some days ago.[Edited by - sapdev on May 6, 2009 7:29:29 AM];  Vertices have to pass a couple of transformations when being rendered. A coarse overview is:(1) from model space (also called object local space) to world space (also called global space),(2) from world space to view space,(3) from view space to normalized device space.(4) from normalized device space to screen space.Points (3) and (4) are done by the PROJECTION matrix and the set-up of glViewPort. Point (1) is done by the MODEL matrix, and point (2) by the VIEW matrix. Now, OpenGL deals with the MODEL and VIEW matrices at once, namely by the MODELVIEW matrix. Hence, the MODELVIEW matrix is a composite of the VIEW and the MODEL matrices like so:V * MThe shown order is correct for column vector matrices as are used by OpenGL.The MODEL matrix portion is responsible to transform geometry that is given w.r.t. the local co-ordinate frame to result in geometry w.r.t. the global co-ordinate frame. Similarly, the VIEW matrix portion is responsible to transform the geometry given w.r.t. the global frame into geometry w.r.t. the virtual camera. In fact, the virtual camera itself can be seen as an object in the global space. If doing so, then the camera's local-to-global transform is just the inverse of the VIEW transform.Further details can be read e.g. in the Red Book of OpenGL, available online.Helper routines like gluLookAt build a VIEW matrix. Now, the VIEW matrix itself can be seen as a composite matrix, namely from composing a translation (to define the position) and a rotation (to define the orientation). If you think of the virtual camera, it is something likeC := TC * RCto position and orientate the camera in the world, and hence the VIEW matrix is something likeV := C-1 = RC-1 * TC-1Due to this and matrix math rules, the VIEW matrix' rotation sub-matrix can simply be isolated by picking the first 3 column vectors. Inverting the matrix is also simple, since the inverse is just the same as the transpose of the matrix (this is true for ortho-normal matrices, and the rotation is such a matrix). What results then is matrix where the 1st column vector is the so-called "right vector" and the 2nd column vector is the so-called "up vector", both w.r.t. the global space. In other words, a vector (1,0,0) in camera local space is the same as the "right vector" in global space, and a vector (0,1,0) in camera local space is the same as the "up vector" in global space. Presumble exactly what you want.Coming to some code, you have to pick the MODELVIEW matrix just before it is multiplied by any MODEL portion. I.e. pick it just after gluLookAt:gluLooakAt(...);GLfloat view[16];glGetFloatv(GL_MODELVIEW_MATRIX, view);Then transpose and isolate the 1st and 2nd column vector, or else don't transpose and isolate the 1st and 2nd row vectors (what has exactly the same result). Notice that OpenGL uses a method called "column major order" to linearize the 2D structure of a matrix in memory. Please seach the GDnet and/or Inet to learn about it.GLfloat right[3];right[0] = view[0];right[1] = view[4];right[2] = view[8];GLfloat up[3];right[0] = view[1];right[1] = view[5];right[2] = view[9];Later, use these vectors as rotation axes instead of (1,0,0) and (0,1,0).The rendering has to look likeglLoadIdentity();gluLooakAt(...);// [1]foreach( object ) {glPushMatrix();glTranslatef(object.pos.x, object.pos.y, object.pos.z);glMultMatrixf(object.orientation);glScalef(object.scale.x, object.scale.y, object.scale.z);// draw the object hereglPopMatrix():)}as was already shown in earlier posts.What's remaining is the problem of constructing the object.orientation matrices, of course. If you want to misuse OpenGL's matrix stack, then you can the following. Each object has a GLfloat orientation[16] field, initialized as identity matrix. Then at the point noted by "// [1]" in the above code snippet, you insert something likeif( activeObject ) {// getting VIEW matrixGLfloat view[16];glGetFloatv(GL_MODELVIEW_MATRIX, view);// isolating "right vector"GLfloat right[3];right[0] = view[0];right[1] = view[4];right[2] = view[8];// isolating "up vector"GLfloat up[3];right[0] = view[1];right[1] = view[5];right[2] = view[9];// computing new orientation matrixglPushMatrix();glLoadIdentity();glRotatef(dx, right[0], right[1], right[2] );glRotatef(dy, up[0], up[1], up[2] );glMultMatrix(activeObject->orientation);glGetFloatv(GL_MODELVIEW_MATRIX, activeObject->orientation);// cleaning upglPopMatrix();}I'm not sure whether the order of the both rotations is correct; you have to experiment a bit. Further, I've written that code here without any testing, so think twice about it. It is likely that it doesn't work ad-hoc, but I hope I have not made a logical mistake.   