yarp

02-24-2004, 11:19 AM

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;

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;