00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00035
00036 #include "SO3SCOL.h"
00037
00038
00039 #include "../SO3SceneGraph/SO3Entity.h"
00040 #include "../SO3SceneGraph/SO3Skeleton.h"
00041 #include "../SO3SceneGraph/SO3Bone.h"
00042 #include "../SO3SceneGraph/SO3Scene.h"
00043
00052 int SO3BoneListFromMesh(mmachine m)
00053 {
00054 #ifdef SO3_DEBUG
00055 MMechostr(MSKDEBUG,"SO3BoneListFromMesh\n");
00056 #endif
00057
00058 int obj = MMget(m, 0);
00059 if(obj==NIL)
00060 {
00061 MMset(m, 0, NIL);
00062 return 0;
00063 }
00064
00065 SNode* node = (SNode*) MMfetch(m, MTOP(obj), 0);
00066 if(node==NULL)
00067 {
00068 MMset(m, 0, NIL);
00069 return 0;
00070 }
00071
00072 if(node->GetNodeType()!=SNode::ENTITY_TYPE_ID)
00073 {
00074 MMset(m, 0, NIL);
00075 return 0;
00076 }
00077
00078 SEntity* curEntity = static_cast<SEntity*> (node);
00079 SSkeleton* skeleton = curEntity->GetSkeleton();
00080 if(skeleton == 0)
00081 {
00082 MMset(m, 0, NIL);
00083 return 0;
00084 }
00085
00086 int p=0;
00087 int tmp_res;
00088 SBoneMap boneListCopy = skeleton->GetBones();
00089 SBoneMap::const_iterator iBones = boneListCopy.begin();
00090 while (iBones != boneListCopy.end());
00091 {
00092 int b = OBJfindTH(m, SO3OBJTYPE, (int)(iBones->second));
00093 if(b!=NIL)
00094 b = MMfetch(m, b, OFFOBJMAG);
00095
00096 if(MMpush(m, b))
00097 return MERRMEM;
00098
00099 INVERT(m, 0, 1);
00100 p++;
00101 iBones++;
00102 }
00103
00104 MMpull(m);
00105 if(MMpush(m, NIL))
00106 return MERRMEM;
00107
00108 for(int j=0; j<p; j++)
00109 {
00110 if(MMpush(m, 2*2))
00111 return MERRMEM;
00112
00113 if(int k=MBdeftab(m))
00114 return k;
00115 }
00116 return 0;
00117 }
00118
00119
00128 int SO3GetRootBoneFromMesh(mmachine m)
00129 {
00130 #ifdef SO3_DEBUG
00131 MMechostr(MSKDEBUG, "SO3GetRootBoneFromMesh\n");
00132 #endif
00133
00134 int obj = MMget(m, 0);
00135 if(obj==NIL)
00136 {
00137 MMset(m, 0, NIL);
00138 return 0;
00139 }
00140
00141 SNode* node = (SNode*) MMfetch(m, MTOP(obj), 0);
00142 if(node==NULL)
00143 {
00144 MMset(m, 0, NIL);
00145 return 0;
00146 }
00147
00148 if(node->GetNodeType()!=SNode::ENTITY_TYPE_ID)
00149 {
00150 MMset(m, 0, NIL);
00151 return 0;
00152 }
00153
00154 SEntity* curEntity = static_cast<SEntity*> (node);
00155 SSkeleton* skeleton = curEntity->GetSkeleton();
00156 if(!skeleton)
00157 {
00158 MMset(m, 0, NIL);
00159 return 0;
00160 }
00161
00162 SBone* rootBone = skeleton->GetRootBone();
00163 if(rootBone==0)
00164 {
00165 MMset(m, 0, NIL);
00166 return 0;
00167 }
00168
00169 int so3obj = OBJfindTH(m, SO3OBJTYPE, (int)(rootBone));
00170 if(so3obj!=NIL)
00171 so3obj = MMfetch(m, so3obj, OFFOBJMAG);
00172
00173 MMset(m, 0, so3obj);
00174 return 0;
00175 }
00176
00177
00187 int SO3GetBoneLocalOrientation(mmachine m)
00188 {
00189 #ifdef SO3_DEBUG
00190 MMechostr(MSKDEBUG, "SO3GetBoneLocalOrientation\n");
00191 #endif
00192
00193 int q = MMpull(m);
00194 int obj = MMget(m, 0);
00195 if((obj==NIL) || (q==NIL))
00196 {
00197 MMset(m, 0, NIL);
00198 return 0;
00199 }
00200
00201 SNode* node = (SNode*) MMfetch(m, MTOP(obj), 0);
00202 if(node==NULL)
00203 {
00204 MMset(m, 0, NIL);
00205 return 0;
00206 }
00207
00208 if(node->GetNodeType()!=SNode::BONE_TYPE_ID)
00209 {
00210 MMset(m, 0, NIL);
00211 return 0;
00212 }
00213
00214 SBone* curBone = static_cast<SBone*> (node);
00215 if(!curBone)
00216 {
00217 MMset(m, 0, NIL);
00218 return 0;
00219 }
00220
00221 int x = MMfetch(m, MTOP(q), 0);
00222 int y = MMfetch(m, MTOP(q), 1);
00223 int z = MMfetch(m, MTOP(q), 2);
00224 int w = MMfetch(m, MTOP(q), 3);
00225
00226 if((x==NIL) || (y==NIL) || (z==NIL) || (w==NIL))
00227 {
00228 MMset(m, 0, NIL);
00229 return 0;
00230 }
00231
00232 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
00233 quat.normalise();
00234 Ogre::Quaternion lquat = curBone->ConvertWorldToLocalOrientation(quat);
00235
00236 int tuple = MMmalloc(m, 4, TYPETAB);
00237 if(tuple==NIL)
00238 {
00239 MMset(m, 0, NIL);
00240 return MERRMEM;
00241 }
00242 MMstore(m, tuple, 0, FTOM((lquat.x)));
00243 MMstore(m, tuple, 1, FTOM((lquat.y)));
00244 MMstore(m, tuple, 2, FTOM((lquat.z)));
00245 MMstore(m, tuple, 3, FTOM((lquat.w)));
00246 MMset(m, 0, PTOM(tuple));
00247
00248 return 0;
00249 }
00250
00251
00255 #define NBBONEPKG 3
00256
00257
00261 char* BONEname[NBBONEPKG]=
00262 {
00263 "SO3BoneListFromMesh",
00264 "SO3GetRootBoneFromMesh",
00265 "SO3GetBoneLocalOrientation"
00266 };
00267
00268
00272 int (*BONEFunc[NBBONEPKG])(mmachine m)=
00273 {
00274 SO3BoneListFromMesh,
00275 SO3GetRootBoneFromMesh,
00276 SO3GetBoneLocalOrientation
00277 };
00278
00279
00283 int BONEnarg[NBBONEPKG]=
00284 {
00285 1,
00286 1,
00287 2
00288 };
00289
00290
00294 char* BONEType[NBBONEPKG]=
00295 {
00296 "fun [SO3_OBJECT] [SO3_OBJECT r1]",
00297 "fun [SO3_OBJECT] SO3_OBJECT",
00298 "fun [SO3_OBJECT [F F F F]] [F F F F]"
00299 };
00300
00301
00307 int SCOLloadBone(mmachine m,cbmachine w)
00308 {
00309 return PKhardpak( m, "Bone", NBBONEPKG, BONEname, BONEFunc, BONEnarg, BONEType );
00310 }
00311
00312
00317 int SCOLfreeBone()
00318 {
00319 return 0;
00320 }
00321