Results 1 to 6 of 6

Thread: Rotating an object around its center

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2004
    Location
    France
    Posts
    10

    Rotating an object around its center

    I've a problem with rotation. I'have objects whose center coordinates is not the origin (0,0,0) but I want them to rotate on themselves. So I calculate the center vertex of each object which gives me a new system coordinate for each object. Then I rotate them. Unfortunately some objects don't rotate exactly around the center vertex. Could someone explain to me how I should proceed ?
    Below is the code.

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(-Center.fX,-Center.fY,-Center.fZ);
    glRotatef(fRot[0],1.0f,0.0f,0.0f);
    glRotatef(fRot[1],0.0f,1.0f,0.0f);
    glRotatef(fRot[2],0.0f,0.0f,1.0f);

    Where center is as follow:

    // Get the max/min
    pVertex= m_mesh.pzVertex;
    vMax.fX = vMin.fX = pVertex->fX;
    vMax.fY = vMin.fY = pVertex->fY;
    vMax.fZ = vMin.fZ = pVertex->fZ;

    for (DWORD dwCnt=0; dwCnt<m_mesh.dwCntVertex; dwCnt++) {
    if (pVertex->fX < vMin.fX)
    vMin.fX = pVertex->fX;
    if (pVertex->fX > vMax.fX)
    vMax.fX = pVertex->fX;
    if (pVertex->fY < vMin.fY)
    vMin.fY = pVertex->fY;
    if (pVertex->fY > vMax.fY)
    vMax.fY = pVertex->fY;
    if (pVertex->fZ < vMin.fZ)
    vMin.fZ = pVertex->fZ;
    if (pVertex->fZ > vMax.fZ)
    vMax.fZ = pVertex->fZ;
    pVertex++;
    }

    // Calculate center of the model
    vCenter.fX = (m_vMax.fX + m_vMin.fX) / 2.0f;
    vCenter.fY = (m_vMax.fY + m_vMin.fY) / 2.0f;
    vCenter.fZ = (m_vMax.fZ + m_vMin.fZ) / 2.0f;

  2. #2
    Senior Member Regular Contributor
    Join Date
    Jan 2004
    Location
    Los Angeles, CA, USA
    Posts
    219

    Re: Rotating an object around its center

    Translate so the object is centered the way you want, then rotate.

  3. #3
    Member Contributor
    Join Date
    Sep 2003
    Location
    Jersey, Channel Islands
    Posts
    66

    Re: Rotating an object around its center

    I think in your code, you should first call the rotate functions and then the translate one. Don't forget to pop the matrix when finished.

    mad

  4. #4
    Junior Member Newbie
    Join Date
    Feb 2004
    Location
    Bangalore,India
    Posts
    25

    Re: Rotating an object around its center

    You are trying to achieve what is known as a composite tranformation.
    Here is the idea to do it:
    1)Translate the entire object to the origin so that the centre of the object and the origin coincide.
    2)Rotate the object now.
    3)Translate the object back to it's original position by the required amount(negative of value specified in step 1).

    You refer to either
    Foley, van Dam, Feiner and Hughes's book on Computer Graphics.OR
    Hearn and Baker's book for the same.
    TOPIC:composite tranformation

    Once you've done that, you could start generating your own tranformation matrices that combine the results of step 1,2 and 3.
    Yup, that's me!!

  5. #5
    Junior Member Newbie
    Join Date
    Jan 2004
    Location
    France
    Posts
    10

    Re: Rotating an object around its center

    Hi princeofflight,
    So "composite transformation" is the key, hu ?
    Thank you for the info, I'm gonna study this and probably buy he books
    I've a lot to learn.

  6. #6
    Junior Member Newbie
    Join Date
    Feb 2004
    Location
    Bangalore,India
    Posts
    25

    Re: Rotating an object around its center

    If u really wanna buy a book-
    I'd really recommend
    Computer Graphics using Opengl- F.S. Hill,Jr.

    which has actual code,

    rather than the ones mentioned above.

    You see, I studied those above mentioned books for Computer Graphics as a theory subject, not as a programming subject.

    Foley is considered the encyclopedia of graphics.

    Besides, u can go through Hearn and Baker's new book on Computer Graphics that I think teaches stuff using opengl.
    Yup, that's me!!

Similar Threads

  1. Rotating an Object around its center.
    By avanindra in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 11-19-2009, 12:04 PM
  2. object to screen center
    By tyua in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 10-13-2009, 08:51 AM
  3. rotating texture about center
    By fodder in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 06-02-2004, 08:57 AM
  4. finding out the object's center
    By infinitecmdz in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 03-04-2004, 03:34 PM
  5. Rotating object around own center?
    By korogui in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 03-13-2003, 06:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean