Parenting lights

View previous topic View next topic Go down

Parenting lights

Post  danisegarra on Mon Oct 26, 2009 12:31 pm

Hello:
I was trying to parent a SIO2lamp with a SIO2object.
I can parent two objects with
_SIO2object2->_SIO2transform->_SIO2parent = _SIO2object->_SIO2transform;
and it works grate.
But if I try to parent a light with
_SIO2lamp->_SIO2transform->_SIO2parent = _SIO2object->_SIO2transform;
it doesn't work.
I tried to create an auxiliar child object and, in every frame, give the position of the object to a light, but it doesn't work because the location of the child objet doesn't change although the parent was moving.
Can anyone help me, please?
Thank you!

danisegarra

Posts : 3
Join date : 2009-10-26

View user profile

Back to top Go down

Re: Parenting lights

Post  sio2interactive on Mon Oct 26, 2009 2:25 pm

Because of OpenGL the lamps have to be rendered this way:

Code:

      memcpy( &l[ 0 ], _SIO2lamp->_SIO2transform->loc, 12 );
      l[ 3 ] = 1.0f;
      
      glLightfv( _SIO2lamp->index, GL_POSITION, &l[ 0 ] );

If you want to parent it to an object, you'll have to do the calculation manually...

_________________
SIO2 Interactive
Free Open Source 3D Game Engine for iPhone and iPod Touch
http://sio2interactive.com
avatar
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 38
Location : Shanghai

View user profile http://sio2interactive.com

Back to top Go down

Re: Parenting lights

Post  danisegarra on Tue Oct 27, 2009 8:26 am

Thank you.
I will try to set the position manualy.
But, also, I need to know how to get the global position of a child object (for non related light issues).
I am checking in tutorial 14 as explains this topic
http://sio2interactive.forumotion.net/sio2-engine-f3/child-object-unchanged-trans-t626.htm
but, like my companions, I didn't found a solution. When asking the location property, it always returns the same value although the parent is moving (rotating, in fact, like an earth-moon system).
Thank you so much.

danisegarra

Posts : 3
Join date : 2009-10-26

View user profile

Back to top Go down

Re: Parenting lights

Post  sio2interactive on Tue Oct 27, 2009 1:20 pm

Location always need to be in world space, the parent child relationship of the matrix have to be relative to the parent.

_________________
SIO2 Interactive
Free Open Source 3D Game Engine for iPhone and iPod Touch
http://sio2interactive.com
avatar
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 38
Location : Shanghai

View user profile http://sio2interactive.com

Back to top Go down

Re: Parenting lights

Post  danisegarra on Wed Oct 28, 2009 9:17 am

Thanks for your replies.
I wrote two functions that allow me to see the global location of a child object.
I hope this will be useful for someone.
Code:

void multMatrix(SIO2transform* pDest, SIO2transform* pOrig2, SIO2transform* pOrig1)
{   
   pDest->mat[0] = pOrig1->mat[0] * pOrig2->mat[0] + pOrig1->mat[4] * pOrig2->mat[1] + pOrig1->mat[8] * pOrig2->mat[2] + pOrig1->mat[12] * pOrig2->mat[3];
   pDest->mat[1] = pOrig1->mat[1] * pOrig2->mat[0] + pOrig1->mat[5] * pOrig2->mat[1] + pOrig1->mat[9] * pOrig2->mat[2] + pOrig1->mat[13] * pOrig2->mat[3];
   pDest->mat[2] = pOrig1->mat[2] * pOrig2->mat[0] + pOrig1->mat[6] * pOrig2->mat[1] + pOrig1->mat[10] * pOrig2->mat[2] + pOrig1->mat[14] * pOrig2->mat[3];
   pDest->mat[3] = pOrig1->mat[3] * pOrig2->mat[0] + pOrig1->mat[7] * pOrig2->mat[1] + pOrig1->mat[11] * pOrig2->mat[2] + pOrig1->mat[15] * pOrig2->mat[3];
   
   pDest->mat[4] = pOrig1->mat[0] * pOrig2->mat[4] + pOrig1->mat[4] * pOrig2->mat[5] + pOrig1->mat[8] * pOrig2->mat[6] + pOrig1->mat[12] * pOrig2->mat[7];
   pDest->mat[5] = pOrig1->mat[1] * pOrig2->mat[4] + pOrig1->mat[5] * pOrig2->mat[5] + pOrig1->mat[9] * pOrig2->mat[6] + pOrig1->mat[13] * pOrig2->mat[7];
   pDest->mat[6] = pOrig1->mat[2] * pOrig2->mat[4] + pOrig1->mat[6] * pOrig2->mat[5] + pOrig1->mat[10] * pOrig2->mat[6] + pOrig1->mat[14] * pOrig2->mat[7];
   pDest->mat[7] = pOrig1->mat[3] * pOrig2->mat[4] + pOrig1->mat[7] * pOrig2->mat[5] + pOrig1->mat[11] * pOrig2->mat[6] + pOrig1->mat[15] * pOrig2->mat[7];
   
   pDest->mat[8] = pOrig1->mat[0] * pOrig2->mat[8] + pOrig1->mat[4] * pOrig2->mat[9] + pOrig1->mat[8] * pOrig2->mat[10] + pOrig1->mat[12] * pOrig2->mat[11];
   pDest->mat[9] = pOrig1->mat[1] * pOrig2->mat[8] + pOrig1->mat[5] * pOrig2->mat[9] + pOrig1->mat[9] * pOrig2->mat[10] + pOrig1->mat[13] * pOrig2->mat[11];
   pDest->mat[10] = pOrig1->mat[2] * pOrig2->mat[8] + pOrig1->mat[6] * pOrig2->mat[9] + pOrig1->mat[10] * pOrig2->mat[10] + pOrig1->mat[14] * pOrig2->mat[11];
   pDest->mat[11] = pOrig1->mat[3] * pOrig2->mat[8] + pOrig1->mat[7] * pOrig2->mat[9] + pOrig1->mat[11] * pOrig2->mat[10] + pOrig1->mat[15] * pOrig2->mat[11];
   
   pDest->mat[12] = pOrig1->mat[0] * pOrig2->mat[12] + pOrig1->mat[4] * pOrig2->mat[13] + pOrig1->mat[8] * pOrig2->mat[14] + pOrig1->mat[12] * pOrig2->mat[15];
   pDest->mat[13] = pOrig1->mat[1] * pOrig2->mat[12] + pOrig1->mat[5] * pOrig2->mat[13] + pOrig1->mat[9] * pOrig2->mat[14] + pOrig1->mat[13] * pOrig2->mat[15];
   pDest->mat[14] = pOrig1->mat[2] * pOrig2->mat[12] + pOrig1->mat[6] * pOrig2->mat[13] + pOrig1->mat[10] * pOrig2->mat[14] + pOrig1->mat[14] * pOrig2->mat[15];
   pDest->mat[15] = pOrig1->mat[3] * pOrig2->mat[12] + pOrig1->mat[7] * pOrig2->mat[13] + pOrig1->mat[11] * pOrig2->mat[14] + pOrig1->mat[15] * pOrig2->mat[15];
}

void applyParentTransform( SIO2transform* pDest, SIO2transform* pOrig, SIO2transform* pParent)
{         
   SIO2transform* PosP = sio2TransformInit();
   PosP->mat[0] = 1; PosP->mat[1] = 0; PosP->mat[2] = 0; PosP->mat[3] = pParent->loc->x;
   PosP->mat[4] = 0; PosP->mat[5] = 1; PosP->mat[6] = 0; PosP->mat[7] = pParent->loc->y;
   PosP->mat[8] = 0; PosP->mat[9] = 0; PosP->mat[10] = 1; PosP->mat[11] = pParent->loc->z;
   PosP->mat[12] = 0;   PosP->mat[13] = 0;   PosP->mat[14] = 0;   PosP->mat[15] = 1;
   
   SIO2transform* Rot1 = sio2TransformInit();
   float cos_v = cosf( pParent->rot->x * SIO2_DEG_TO_RAD );
   float sin_v = sinf( pParent->rot->x * SIO2_DEG_TO_RAD );
   Rot1->mat[0] = 1;   Rot1->mat[1] = 0; Rot1->mat[2] = 0; Rot1->mat[3] = 0;
   Rot1->mat[4] = 0; Rot1->mat[5] = cos_v; Rot1->mat[6] = -sin_v; Rot1->mat[7] = 0;
   Rot1->mat[8] = 0; Rot1->mat[9] = sin_v; Rot1->mat[10] = cos_v; Rot1->mat[11] = 0;
   Rot1->mat[12] = 0;   Rot1->mat[13] = 0;   Rot1->mat[14] = 0;   Rot1->mat[15] = 1;
   
   SIO2transform* Rot2 = sio2TransformInit();
   cos_v = cosf( pParent->rot->y * SIO2_DEG_TO_RAD );
   sin_v = sinf( pParent->rot->y * SIO2_DEG_TO_RAD );
   Rot2->mat[0] = cos_v; Rot2->mat[1] = 0; Rot2->mat[2] = sin_v; Rot2->mat[3] = 0;
   Rot2->mat[4] = 0; Rot2->mat[5] = 1; Rot2->mat[6] = 0; Rot2->mat[7] = 0;
   Rot2->mat[8] = -sin_v; Rot2->mat[9] = 0; Rot2->mat[10] = cos_v; Rot2->mat[11] = 0;
   float aux = Rot2->mat[8];
   Rot2->mat[12] = 0;   Rot2->mat[13] = 0;   Rot2->mat[14] = 0;   Rot2->mat[15] = 1;
   
   SIO2transform* Rot3 = sio2TransformInit();
   cos_v = cosf( pParent->rot->z * SIO2_DEG_TO_RAD );
   sin_v = sinf( pParent->rot->z * SIO2_DEG_TO_RAD );
   Rot3->mat[0] = cos_v; Rot3->mat[1] = -sin_v; Rot3->mat[2] = 0; Rot3->mat[3] = 0;
   Rot3->mat[4] = sin_v; Rot3->mat[5] = cos_v; Rot3->mat[6] = 0; Rot3->mat[7] = 0;
   Rot3->mat[8] = 0; Rot3->mat[9] = 0; Rot3->mat[10] = 1; Rot3->mat[11] = 0;
   Rot3->mat[12] = 0;   Rot3->mat[13] = 0;   Rot3->mat[14] = 0;   Rot3->mat[15] = 1;

   
   SIO2transform* pAux0 = sio2TransformInit();
   SIO2transform* pAux1 = sio2TransformInit();
   SIO2transform* pAux2 = sio2TransformInit();
   multMatrix(pAux0, PosP, Rot1);
   multMatrix(pAux1, pAux0, Rot2);
   multMatrix(pAux2, pAux1, Rot3);
      
   float fX = pAux2->mat[0]*pOrig->loc->x + pAux2->mat[1]*pOrig->loc->y + pAux2->mat[2]*pOrig->loc->z;
   float fY = pAux2->mat[4]*pOrig->loc->x + pAux2->mat[5]*pOrig->loc->y + pAux2->mat[6]*pOrig->loc->z;
   float fZ = pAux2->mat[8]*pOrig->loc->x + pAux2->mat[9]*pOrig->loc->y + pAux2->mat[10]*pOrig->loc->z;
       
   pDest->loc->x = fX;
   pDest->loc->y = fY;
   pDest->loc->z = fZ;
      
   sio2TransformFree(Rot1);
   sio2TransformFree(Rot2);
   sio2TransformFree(Rot3);
   sio2TransformFree(pAux0);
   sio2TransformFree(pAux1);
   sio2TransformFree(pAux2);
}
The first one, multMatrix write in the matrix of pDest the result of pOrig2*pOrig1.
The second one is to give to a pDest transform the result of aply the transform of pParent to pOrig.
Using applyParentTransform I can see in the pDest transform, the global position of pOrig.

But I still have a problem.
In my game, I'm using rigid bodies (_SIO2object->_SIO2objectphysic->_btRigidBody). And I'm moving it by applying torques to it.
The problem to apply torques is that the rotation in SIO2transform->rot is going crazy (for example, aplying an x,0,0 torque, the rotation changes also in y axis) and I can't apply my functions correctly.
Has anyone know how to convert the torque rotation to a standard rotation (or extract the actual standard rotation from the object)?
Thanks so much!

danisegarra

Posts : 3
Join date : 2009-10-26

View user profile

Back to top Go down

Re: Parenting lights

Post  Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum