Thread: OpenGL Rotation-- Part 2

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    11

    Smile OpenGL Rotation-- Part 2

    Hi all!!
    This is yet another rotation problem.. Once the image is displayed i m rotating on screen. If i move my move left or right then i captured that and rotate using
    glRotated(captured_angleX , 0.0,1.0,0.0);

    similarly if i move up or down i rotate using

    glRotated(captured_angleY , 1.0,0.0,0.0);

    IT WORKS VERY WELL.. But The problem is I want to rotate with respect to Z-axis. How to do it?? If my question is not clear jus ping me i will elaborate the problem statement.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    I have not used openGL, but I assume it would be something like this:
    glRotated(value , 0.0,0.0,1.0);

    I think you have X and Y mixed up as well.

    OpenGL rotation

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    11
    thats ok. But how to calculate that value(Angle)??

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    I assume that if you want to rotate 90 degrees, value = 90

  5. #5
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    I think what your asking is, "I am already calculating a given angle of rotation on the X and Y axes based off of mouse movement, how can I also calculate the angle of rotation on the Z axis based off of the same mouse movement?"

    Short answer: you can't. Mouse movement is 2 dimensional, and so extracting a 3rd dimension of data from it is extremely difficult to do (if not impossible). Some research has been going on in this field however (research on extracting 3d information from a 2d model) and significant advances have been made at Stanford. Check the link out...it's way cool stuff.

    But anyways, back to your question. You basically need to provide a 3rd way for the user to input data. One of the most simple ways is to use keyboard input. Bind two keys on the keyboard to the rotation on the Z-axis. For now let's say we use the keys Q and W. If the user presses Q it will increase the angle of rotation on the Z-axis, and if the user presses W it will decrease the angle of rotation. There are a myriad of other ways you could do it. You could have the user hold down the mouse wheel, and then you could interpret any mouse movement while the mouse wheel is held down as rotating along the Z-axis.

    But the basic jist of it is that you can't just do it based off of your current 2-dimensional mouse movement data.
    My Website

    "Circular logic is good because it is."

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Using the mouse wheel for Z rotation would be a bit counter-intuitive. I don't quite understand why you care about Z rotation since in most systems this is a roll operation about the z axis. Z rotations are not all that useful or at least I've not found them to be. It may be possible to extract the Z rotation by turning the x and y motions of the mouse into vectors, crossing them, and normalizing. However this will also yield some wacky results. Microsoft has an arcball class that turns mouse movements into a quaternion so you may want to look in their SDK for this sample on how to do it.

    Another problem is you are using Euler angles. If you imagine Euler angles as 3 concentric rings mounted about a center point you quickly realize something. There is a point at which all 3 rings can line up with one another. It is at this point you will experience gimbal lock as one axis of rotation is mapped onto another axis. Anything you do on one axis will now affect another axis. There are several ways to overcome this. You can perform axis-angle rotations which are very similar in nature to quaternions or you can just use quaternions. The disadvantage to using quaternions is that graphics APIs want their rotations expressed as matrices. So it takes a bit of math to express the quaternion in matrix form. The advantages to quaternions are many. You can use SLERP or spherical linear interpolation which will always smoothly interpolate from one quaternion to another - or more simply one 3D orientation to another. Trying to do this using Euler angles is an exercise in frustration. Euler angles do not LERP well and simply cannot SLERP as easily as quaternions. Quaternions also offer quadratic slerp or SQUAD although my experience with SQUAD is quite limited.

    A quick test to determine if you are gimbal locked is to pitch the camera straight up to 90 degrees or PI/4. Now attempt to yaw or rotate about the y axis. If you are not using axis angle rotations you will not yaw but will roll. As you roll you will also find a point at which a rotation about the x axis does not yield the expected results. Problem is that the more you pitch the more the yaw is mapped onto another axis. So at 45 degrees up pitch you are beginning to map onto another axis of rotation. The situation gets out of hand very quickly.
    Last edited by VirtualAce; 05-23-2008 at 09:52 PM.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    11
    "You could have the user hold down the mouse wheel, and then you could interpret any mouse movement while the mouse wheel is held down as rotating along the Z-axis".. How to do it DavidP????

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    11
    One More Problem: When i move my mouse along X-axis or Y-axis at a time my rotation is perfect.. Suppose if i move my mouse diagonally where Both X and Y point changes, my rotation is not working properly.. To be clear the rotation is not clear.. its like dragging the object here and there. Kindly suggest some ideas If any...

  9. #9
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    How to do it DavidP????
    Some if-statements, an event handler, and a couple of boolean variables? I assume it should integrate with whatever method you are using to currently catch your mouse movement.

    You might want to make a camera class. Here is a good tutorial:

    http://www.flipcode.com/archives/OpenGL_Camera.shtml
    My Website

    "Circular logic is good because it is."

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    11
    lemme try n ping u back...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 04:16 PM
  2. rotation matrix
    By SAMSAM in forum Game Programming
    Replies: 4
    Last Post: 03-02-2003, 01:54 PM
  3. OpenGL .dll vs video card dll
    By Silvercord in forum Game Programming
    Replies: 14
    Last Post: 02-12-2003, 07:57 PM
  4. MISC questions about OpenGL
    By Silvercord in forum Game Programming
    Replies: 12
    Last Post: 01-25-2003, 04:20 PM
  5. What is it with OpenGL tutorials?
    By Eibro in forum Game Programming
    Replies: 22
    Last Post: 01-12-2003, 04:49 PM