Thread: OpenGL rotation

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69

    OpenGL rotation

    I'm trying to make a little 2D rectangle rotate. But, it can also move up. When it does move up, it rotates around the center of the window, like its in orbit. I want it to rotate on its own center point help! I've been using
    Code:
    glRotate2f(x, 0.0f, 0.0f 1.0f);

  2. #2
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    Is that the actual call you are usingd? It shouldn't compile.

    Something like
    Code:
    glRotate3f(x, 0.0, 0.0)
    should work for your case. The number does not correspond to how many things you're passing, and there's a comma missing.
    The last param, you can omit, because it defaults to 1 if not used.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    It is
    Code:
    glRotatef(angle, x, y, z);

  4. #4
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    i think I read something that glRotate() rotates the whole plane, not just the object. how do i make only the object rotate. If you give me any math equations, could you explain them please, im only in grade 9.

  5. #5
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Code:
    /*draw stuff*/
    glTranslatef(0.0,x,0.0);/*move up like you said*/
    glPushMatrix();
         glRotatef(x,0.0,1.0,0.0);
         /*draw stuff to rotate*/
    glPopMatrix();
    you have to get used to using push pop matrix like that. Or else you are rotating everything. Does this help if not show me the code.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    OpenGL rotates the coordinate system. If you rotate first, then translate, the direciton it translates is now with respect to the *rotated* coordinate system.

    Say, for example, you want to rotate 45 degrees about the z axis, then you wan to translate down the x axis 5 units. Well, if you *just* translated down the x axis 5 units, then your position would be:

    (5,0,0)

    However, if you rotate 45 degrees about the z axis (glRotatef(45,0,0,1)) and THEN translate down the x axis 5 units (glTranslatef(5,0,0)) your position will *actually* be about:

    (3.5, 3.5, 0)

    So, in general, if you want an object to *just* rotate, you need to translate it *first*, and then rotate the object. Linuxdude's code works, but you don't need to push and then pop the matrix, just as long as you always translate before you rotate.

    Note that there are some times when it is convenient to do it the other way around. For example, if you want a planet to orbit another one, you can just rotate out through the current angle and then always translate down a constant axis (because that axis will actually be rotated if you call glRotate first).

    If something I or someone else has said (we're all saying basically the same thing) doesn't make sense, just say what doesn't make sense and we'll reiterate or draw some pics. Good luck.
    Last edited by Darkness; 01-30-2005 at 11:08 AM.
    See you in 13

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Unless you already knew, matrix transformations take effect in a reverse order. If you call Translate then Rotate the rotation will be made first, then the translation. Quite mysterious at first, but once you use push/pop-matrix to its full power it'll seem natural.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    To be honest I'm kind of confused right now and something I said earlier might not be correct.

    EDIT:
    I didn't really say anything that was incorrect on the last page, but I might've been a bit ambiguous. I am sorry to say that this is probably all very confusing. Just in general, if you want an object to look like it is rotating around itself, always call glTranslate first, and then call glRotate. However, technically speaking, what it does is it rotates the object first, and then translates it out to the world position, but dont' worry about those details until you get used to doing basic first.

    glTranslate(pos.x,pos.y,pos.z);

    glRotate(angle,axis.x,axis.y,axis.z);

    that will always have the effect of an object rotating around itself
    Last edited by Darkness; 01-31-2005 at 08:15 AM.
    See you in 13

  9. #9
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    Thanks darkness, I figured it out
    Code:
    float temp;
    temp = theta * (3.14 / 180);
    side = side + (inc * cos(temp));
    up = up + (inc * sin(temp));
    glTranslate(side, up, 0.0f);
    You've been a lot of help

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    Very nice work. If you can continue to work problems out on your own you *will* be successful with this stuff!

    I was honestly afriad I had confused you (because admittidely some of the stuff I was saying was ambiguous and confusing).

    Keep up the good work.
    See you in 13

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL hue rotation
    By Blink in forum Game Programming
    Replies: 4
    Last Post: 08-14-2007, 03:42 PM
  2. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. OpenGL .dll vs video card dll
    By Silvercord in forum Game Programming
    Replies: 14
    Last Post: 02-12-2003, 07:57 PM