PDA

View Full Version : Rotating an object around its center



yarp
02-24-2004, 12:19 PM
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;

endash
02-24-2004, 12:52 PM
Translate so the object is centered the way you want, then rotate.

madmortigan
02-24-2004, 02:23 PM
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

princeoflightning
02-25-2004, 02:54 AM
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.

yarp
02-25-2004, 06:20 AM
Hi princeofflight,
So "composite transformation" is the key, hu ?
Thank you for the info, I'm gonna study this and probably buy he books http://www.opengl.org/discussion_boards/ubb/wink.gif
I've a lot to learn.

princeoflightning
02-26-2004, 04:36 AM
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.