3DS File & local coordinate systems

A place for Ogre users to discuss non-Ogre subjects with friends from the community.
Post Reply
mad god
Gnoblar
Posts: 22
Joined: Mon May 18, 2009 4:05 am

3DS File & local coordinate systems

Post by mad god »

Hello everybody.
This is not really related to Ogre, but I though you guys had encountered this before.
I'm trying to make 3DS importer at the moment. I now read all the necessary information, but it looks like I have not anough brain to use it. :)

The problem is the following (quite common, I think, but was not able to find a solution for quite a while):
1. 3DS has tree-like structure of model parts for animation. Each part (object) has its own Local Coordinate system which is basically 3 Axis Vectors and and Origin.
2. Suppose I have just two objects and the second one is a child of the first one, so when the first one is rotated/translated/scaled the second one should be rotated/translated/scaled as well (as far as I understand).

Now I just do not know what to do with those Local Coordinate systems to display the whole model correctly.

I was trying different approaches, but none of them proved to be right.

One of them is:

// world coordinate system is identity
D3DXMATRIX mWorld;
D3DXMatrixIdentity(&mWorld);

// Create a matrix 4x4 using LCS info:
D3DXMATRIX mLocal(
pObj->sLCS.sAxis1.x, pObj->sLCS.sAxis2.x, pObj->sLCS.sAxis3.x, 0,
pObj->sLCS.sAxis1.y, pObj->sLCS.sAxis2.y, pObj->sLCS.sAxis3.y, 0,
pObj->sLCS.sAxis1.z, pObj->sLCS.sAxis2.z, pObj->sLCS.sAxis3.z, 0,
0, 0, 0, 1);

// Then invert it:
D3DXMATRIX mLocalInv;
D3DXMatrixInverse(&mLocalInv, NULL, &mLocal);

// Calculate the coordinate system transition matrix
D3DXMATRIX mRot = mWorld * mLocalInv;

// Then I transformed vertices
D3DXVec3TransformCoordArray(...)

The result is even worse than not transforming them at all.

The questions are:
1. What is wrong and where do I fit the LCS's Origin?
2. What do I do with parent-child relation? Should I apply parents transformation matrix to the child first and then apply child's transformation matrix? Or should I do something else entirely.

Thank you in advance for any hints. This stuff drives me crazy.
mad god
Gnoblar
Posts: 22
Joined: Mon May 18, 2009 4:05 am

Re: 3DS File & local coordinate systems

Post by mad god »

I also tryed this for every part(object):
This is recursive function, applies parent's first.
Does not work :(

void ApplyObjectLCS(Raw3DSObject *pObj, Raw3DSObject *pObjectsArray, D3DXVECTOR3 *pv, long nVertexCount)
{
// First apply father
if(pObj->nFatherObject >= 0)
ApplyObjectLCS(pObjectsArray + pObj->nFatherObject, pObjectsArray, pv, nVertexCount);

D3DXMATRIX mWorld;
D3DXMatrixIdentity(&mWorld);

// Calculate the inverse of the local matrix
D3DXMATRIX mLocal(pObj->sLCS.sAxis1.x, pObj->sLCS.sAxis2.x, pObj->sLCS.sAxis3.x, 0,
pObj->sLCS.sAxis1.y, pObj->sLCS.sAxis2.y, pObj->sLCS.sAxis3.y, 0,
pObj->sLCS.sAxis1.z, pObj->sLCS.sAxis2.z, pObj->sLCS.sAxis3.z, 0,
0, 0, 0, 1);
D3DXMATRIX mLocalInv;
D3DXMatrixInverse(&mLocalInv, NULL, &mLocal);

// Calculate the coordinate system transition matrix
D3DXMATRIX mRot = mWorld * mLocalInv;

// Shift vertices first to the origin is the new center
D3DXVECTOR3 *pvCurEnd = pv + nVertexCount;
for(D3DXVECTOR3 *pvCur = pv; pvCur != pvCurEnd; ++pvCur)
*pvCur -= pObj->sLCS.sOrigin;

// Transform vertices from local coordinate system to world
D3DXVec3TransformCoordArray(pv, sizeof(D3DXVECTOR3), pv, sizeof(D3DXVECTOR3), &mRot, nVertexCount);

// Shift vertices back
for(pvCur = pv; pvCur != pvCurEnd; ++pvCur)
*pvCur += pObj->sLCS.sOrigin;
}
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Re: 3DS File & local coordinate systems

Post by jacmoe »

I'm moving you to Off-Topic.. :wink:
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
Post Reply