   help with relative 3D rotation
  Hello,  I have an iphone app which displays a 3D model.  When I drag on the screen, I want the model to rotate in a sensible way.  As in, drag to the right and the model rotates to the right.  Drag up, it rotates up.  I've been using 3 variables (xRot, yRot, zRot) to build my matrix in openGl, like this:glLoadIdentity();glGetFloatv(GL_MODELVIEW_MATRIX, modelMatrix);glRotatef(xRot, modelMatrix[1], modelMatrix[5], modelMatrix[9]);glGetFloatv(GL_MODELVIEW_MATRIX, modelMatrix);glRotatef(-yRot, modelMatrix[0], modelMatrix[4], modelMatrix[8]);glGetFloatv(GL_MODELVIEW_MATRIX, modelMatrix);glRotatef(-zRot, modelMatrix[2], modelMatrix[6], modelMatrix[10]);glScalef(drawScale,drawScale,drawScale);glTranslatef(xTrans, yTrans, zTrans);When I get a screen drag, it is represented by the 2D vector P.x, P.y.  As long as zRot is 0, then all I have needed to do is add P.x and -P.y to xRot and yRot respectively.  The model rotates like it should.However, if zRot is nonzero then it doesn't work any more.  The zRot variable causes a rotation around the z axis, like the hands of a clock.  I was thinking that what I needed to do was rotate P by zRot before applying it to the xRot, yRot variables, but that did not work.Can anyone help me out?  I am very rusty with my 3D math.  Is it possible to do what I want simply, or does adding that third rotation complicate things so much that my simple scheme here won't work anymore?Thanks for any tips!Bob   
  This sounds like arcball rotation. On Nehe is a tutorial: clicky;  You should read this:Eulers are evil;Quote:Original post by venzonYou should read this:Eulers are evilI second that Euler angles are mostly evil. But IMHO there are some mistakes belonging to quaternions on that page. Just for the case that the OP is about to try quaternions:Quote:Eulers are evilQuaternions ... are sets of four numbers that can be thought of as being an (X,Y,Z) vector of length 1.0 - and an angle to rotate about that vector.I don't know anybody who is able to imagine a quaternion. It is just an abstract mathematical concept. It also lacks the simple geometrical interpretation of the 4 parameters of glRotate (which in fact denote an axis/angle pair rotation representation).Quote:Eulers are evilyet (like Euler angles) can ONLY encode rotationNope. A quaternion encodes a pure rotation if and only if it has unit length, so it has to be a unit-quaternion for rotations. That is the reason why also quaternions will need to be normalized if enough concatenations are done; otherwise you will get some deformation and not only a deviating rotation.Quote:Eulers are evilyou do have to ensure the length of the vector part stays nailed at 1.0 at all timesThat would violate the unit length requirement, because the definition of the "vector part" as understood from the article is just[ x*sin(a/2) y*sin(a/2) z*sin(a/2) ]so that the length is computed to( x2 + y2 + z2 ) * sin(a/2)2 = sin(a/2)2because the original axis is normalized. Instead the entire quaternion has to be normalized:| [ cos(a/2) x*sin(a/2) y*sin(a/2) z*sin(a/2)] | == 1Quote:Eulers are evil... but they [Quaternions] are pretty cool since (like matrices) they encode rotation in an order-independent mannerA quaternion defines a single rotation (the long and short arc problem edged out) about an arbitrary (direction-wise) axis, and hence has no relation to a rotation order at all. As soon as quaternion rotations are concatenated the well known order problems arise as well.   