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
00036
00037 #include "SO3SCOL.h"
00038
00039
00040 #include "../SO3Animation/SO3Anim.h"
00041 #include "../SO3Material/SO3Material.h"
00042 #include "../SO3SceneGraph/SO3Entity.h"
00043 #include "../SO3SceneGraph/SO3Scene.h"
00044 #include "../SO3SceneGraph/SO3Skeleton.h"
00045
00046
00055 int SO3EntityNumOfSubEntities(mmachine m)
00056 {
00057 #ifdef SO3_DEBUG
00058 MMechostr(MSKDEBUG, "SO3EntityNumOfSubEntities\n");
00059 #endif
00060
00061 int obj = MMget(m, 0);
00062 if(obj==NIL)
00063 {
00064 MMset(m, 0, NIL);
00065 return 0;
00066 }
00067
00068 SNode* node = (SNode*) MMfetch(m, MTOP(obj), 0);
00069 if(node==NULL)
00070 {
00071 MMset(m, 0, NIL);
00072 return 0;
00073 }
00074
00075 if(node->GetNodeType()!=SNode::ENTITY_TYPE_ID)
00076 {
00077 MMset(m, 0, NIL);
00078 return 0;
00079 }
00080
00081 SEntity* curEntity = static_cast<SEntity*> (node);
00082 MMset(m, 0, ITOM(curEntity->GetNumSubEntities()));
00083 return 0;
00084 }
00085
00095 int SO3EntitySetVisibilityFlags(mmachine m)
00096 {
00097 #ifdef SO3_DEBUG
00098 MMechostr(MSKDEBUG, "SO3EntitySetVisibilityFlags\n");
00099 #endif
00100
00101 int flags = MMpull(m);
00102 int obj = MMget(m, 0);
00103 if(obj==NIL)
00104 {
00105 MMset(m, 0, NIL);
00106 return 0;
00107 }
00108
00109 SNode* node = (SNode*) MMfetch(m, MTOP(obj), 0);
00110 if(node==NULL)
00111 {
00112 MMset(m, 0, NIL);
00113 return 0;
00114 }
00115
00116 if(node->GetNodeType()!=SNode::ENTITY_TYPE_ID)
00117 {
00118 MMechostr(MSKDEBUG, "Not an Entity type");
00119 MMset(m, 0, NIL);
00120 return 0;
00121 }
00122
00123 SEntity* curEntity = static_cast<SEntity*> (node);
00124
00125 if (flags == NIL)
00126 flags = -1;
00127 else
00128 flags = MTOI(flags);
00129
00130 curEntity->SetVisibilityFlags(flags);
00131
00132 MMset(m, 0, ITOM(0));
00133 return 0;
00134 }
00135
00144 int SO3EntityGetVisibilityFlags(mmachine m)
00145 {
00146 #ifdef SO3_DEBUG
00147 MMechostr(MSKDEBUG, "SO3EntityGetVisibilityFlags\n");
00148 #endif
00149
00150 int obj = MMget(m, 0);
00151 if(obj==NIL)
00152 {
00153 MMset(m, 0, NIL);
00154 return 0;
00155 }
00156
00157 SNode* node = (SNode*) MMfetch(m, MTOP(obj), 0);
00158 if(node==NULL)
00159 {
00160 MMset(m, 0, NIL);
00161 return 0;
00162 }
00163
00164 if(node->GetNodeType()!=SNode::ENTITY_TYPE_ID)
00165 {
00166 MMset(m, 0, NIL);
00167 return 0;
00168 }
00169
00170 SEntity* curEntity = static_cast<SEntity*> (node);
00171
00172 MMset(m, 0, ITOM(curEntity->GetVisibilityFlags()));
00173 return 0;
00174 }
00175
00185 int SO3EntityGetMaterialByIndex(mmachine m)
00186 {
00187 #ifdef SO3_DEBUG
00188 MMechostr(MSKDEBUG,"SO3EntityGetMaterialByIndex\n");
00189 #endif
00190
00191 int idx = MMpull(m);
00192 int obj = MMget(m, 0);
00193 if((obj == NIL) || (idx == NIL) || ((MTOI(idx)) < 0))
00194 {
00195 MMset(m, 0, NIL);
00196 return 0;
00197 }
00198
00199 SNode* node = (SNode*) MMfetch(m, MTOP(obj), 0);
00200 if(node==NULL)
00201 {
00202 MMset(m, 0, NIL);
00203 return 0;
00204 }
00205
00206 if(node->GetNodeType()!=SNode::ENTITY_TYPE_ID)
00207 {
00208 MMset(m, 0, NIL);
00209 return 0;
00210 }
00211
00212 unsigned int uidx = MTOI(idx);
00213
00214 SEntity* curEntity = static_cast<SEntity*> (node);
00215 if(uidx >= curEntity->GetNumSubEntities())
00216 {
00217 MMset(m, 0, NIL);
00218 return 0;
00219 }
00220
00221 SMaterial* tmpMat = curEntity->GetParentScene()->GetMaterial(curEntity->GetGroupName(), curEntity->getOgreEntityPointer()->getSubEntity(uidx)->getMaterial()->getName());
00222 if (tmpMat == 0)
00223 {
00224 MMset(m, 0, NIL);
00225 return 0;
00226 }
00227
00228
00229 MMpull(m);
00230 return createOrRetrieveScolMaterialAndSendToVM(m, curEntity->GetParentScene(), tmpMat);
00231 }
00232
00233
00242 int SO3EntityMaterialList(mmachine m)
00243 {
00244 #ifdef SO3_DEBUG
00245 MMechostr(MSKDEBUG, "SO3EntityMaterialList\n");
00246 #endif
00247
00248 int ent = MMget(m, 0);
00249 if (ent==NIL)
00250 {
00251 MMechostr(MSKDEBUG, "Entity is NULL\n");
00252 MMset(m, 0, NIL);
00253 return 0;
00254 }
00255
00256 SNode* node = (SNode*) MMfetch(m, MTOP(ent), 0);
00257 if (node==NULL)
00258 {
00259 MMset(m, 0, NIL);
00260 return 0;
00261 }
00262
00263 if (node->GetNodeType() != SNode::ENTITY_TYPE_ID)
00264 {
00265 MMset(m, 0, NIL);
00266 return 0;
00267 }
00268
00269 SEntity* entity = static_cast<SEntity*>(node);
00270
00271
00272
00273 int nbMat = entity->GetNumSubEntities();
00274 int k;
00275 int tmp_res, n = 0;
00276 for (int i=0; i<nbMat; i++)
00277 {
00278 SMaterial* material = node->GetParentScene()->GetMaterial(entity->GetGroupName(), entity->getOgreEntityPointer()->getSubEntity(i)->getMaterial()->getName());
00279 if(material == NULL)
00280 {
00281 MMpush(m, NIL);
00282 return 0;
00283 }
00284
00285 createOrRetrieveScolMaterialAndSendToVM(m, node->GetParentScene(), material);
00286 INVERT(m, 0, 1);
00287 n++;
00288 }
00289
00290 MMpull(m);
00291 if (MMpush(m, NIL))
00292 return MERRMEM;
00293
00294 for(int j=0; j<n; j++)
00295 {
00296 if (MMpush(m,2*2))
00297 return MERRMEM;
00298
00299 if (k=MBdeftab(m))
00300 return k;
00301 }
00302 return 0;
00303 }
00304
00305
00315 int SO3EntityAttachSkeleton(mmachine m)
00316 {
00317 #ifdef SO3_DEBUG
00318 MMechostr(MSKDEBUG, "SO3EntityAttachSkeleton\n");
00319 #endif
00320
00321 int p = MMpull(m);
00322 int ent = MMpull(m);
00323
00324 if ((p==NIL)||(ent==NIL))
00325 {
00326 MMpush(m, NIL);
00327 return 0;
00328 }
00329
00330 SNode* node = (SNode*) MMfetch(m, MTOP(ent), 0);
00331 if (node==NULL)
00332 {
00333 MMpush(m, NIL);
00334 return 0;
00335 }
00336
00337 if (node->GetNodeType() != SNode::ENTITY_TYPE_ID)
00338 {
00339 MMpush(m, NIL);
00340 return 0;
00341 }
00342 SEntity* entity = static_cast<SEntity*> (node);
00343 SScene* scene = entity->GetParentScene();
00344 string pathStd = MMstartstr(m, MTOP(p));
00345
00346 for(unsigned int i=0; i<pathStd.length(); i++)
00347 {
00348 if(pathStd.substr(i,1) == "\\")
00349 pathStd.replace(i,1,"/");
00350 }
00351
00352 try
00353 {
00354 Ogre::SkeletonPtr sklptr = (Ogre::SkeletonPtr)scene->O3SkeletonManager->getByName(pathStd, entity->GetGroupName());
00355 if(!sklptr.isNull())
00356 {
00357
00358 entity->getOgreEntityPointer()->getMesh()->_notifySkeleton(sklptr);
00359 }
00360 else
00361 {
00362
00363 entity->getOgreEntityPointer()->getMesh()->setSkeletonName(pathStd);
00364 }
00365
00366
00367 Ogre::Entity* ogreEntity = entity->getOgreEntityPointer();
00368 ogreEntity->_initialise(true);
00369 entity->Reload();
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409 string tmpSkeletonName = entity->GetName() +"."+ pathStd;
00410 SSkeleton* newSkeleton = 0;
00411 try
00412 {
00413 newSkeleton = scene->CreateSkeleton(tmpSkeletonName, entity);
00414
00415
00416 SBoneMap bonesListCopy = newSkeleton->GetBones();
00417 SBoneMap::iterator iBone = bonesListCopy.begin();
00418 while (iBone != bonesListCopy.end())
00419 {
00420 SBone* newBone = iBone->second;
00421 assert(newBone != 0);
00422 createBone(m, (SNode*)newBone, scene, entity);
00423
00424 MMpull(m);
00425
00426 iBone++;
00427 }
00428
00429
00430 SAnimMap animationsListCopy = newSkeleton->GetAnimations();
00431 SAnimMap::iterator iAnimation = animationsListCopy.begin();
00432 while(iAnimation != animationsListCopy.end())
00433 {
00434 SAnim* newAnimation = iAnimation->second;
00435 assert(newAnimation != 0);
00436 createAnim(m, newAnimation, entity);
00437
00438 MMpull(m);
00439
00440 SAnimTrackList animationsTracksListCopy = newAnimation->GetAnimationsTracks();
00441 SAnimTrackList::iterator iAnimationTrack = animationsTracksListCopy.begin();
00442 while(iAnimationTrack != animationsTracksListCopy.end())
00443 {
00444 SAnimTrack* newAnimationTrack = (*iAnimationTrack);
00445 assert(newAnimationTrack != 0);
00446 createAnimTrack(m, newAnimationTrack, newAnimation);
00447
00448 MMpull(m);
00449
00450 iAnimationTrack++;
00451 }
00452 iAnimation++;
00453 }
00454 }
00455 catch(Ogre::Exception &e)
00456 {
00457 MMechostr(MSKDEBUG, "An exception has occurred: %s\n", e.what());
00458 MMpush(m, NIL);
00459 return 0;
00460 }
00461
00462
00463 MMpush(m, ent);
00464 return 0;
00465 }
00466 catch(Ogre::Exception &e)
00467 {
00468 MMechostr(MSKDEBUG, "An exception has occurred: %s\n", e.what());
00469 MMpush(m, NIL);
00470 return 0;
00471 }
00472 }
00473
00474
00485 int SO3EntitySetMaterial(mmachine m)
00486 {
00487 #ifdef SO3_DEBUG
00488 MMechostr(MSKDEBUG, "SO3EntitySetMaterial\n");
00489 #endif
00490
00491 int index = MMpull(m);
00492 int mat = MMpull(m);
00493 int ent = MMget(m, 0);
00494 if((ent == NIL)||(mat == NIL))
00495 {
00496 MMset(m, 0, NIL);
00497 return 0;
00498 }
00499
00500 SMaterial* material = (SMaterial*) MMfetch(m, MTOP(mat), 0);
00501 if (material==NULL)
00502 {
00503 MMset(m, 0, NIL);
00504 return 0;
00505 }
00506
00507 SNode* mNode = (SNode*) MMfetch(m, MTOP(ent), 0);
00508 if (mNode->GetNodeType() != SNode::ENTITY_TYPE_ID)
00509 {
00510 MMset(m, 0, NIL);
00511 return 0;
00512 }
00513 SEntity* entity = static_cast <SEntity*> (mNode);
00514
00515
00516 if((index == NIL)||((MTOI(index)) < 0))
00517 entity->getOgreEntityPointer()->setMaterialName(material->GetName(), material->GetGroupName());
00518 else
00519 {
00520 unsigned int uidx = MTOI(index);
00521 if(uidx<entity->GetNumSubEntities())
00522 entity->getOgreEntityPointer()->getSubEntity(uidx)->setMaterialName(material->GetName(), material->GetGroupName());
00523 }
00524
00525 MMset(m, 0, ITOM(1));
00526 return 0;
00527 }
00528
00529
00533 #define NBENTITYPKG 7
00534
00535
00539 char* ENTITYname[NBENTITYPKG]=
00540 {
00541 "SO3EntityNumOfSubEntities",
00542 "SO3EntityGetMaterialByIndex",
00543 "SO3EntityMaterialList",
00544 "SO3EntityAttachSkeleton",
00545 "SO3EntitySetMaterial",
00546 "SO3EntitySetVisibilityFlags",
00547 "SO3EntityGetVisibilityFlags"
00548 };
00549
00550
00554 int (*ENTITYFunc[NBENTITYPKG])(mmachine m)=
00555 {
00556 SO3EntityNumOfSubEntities,
00557 SO3EntityGetMaterialByIndex,
00558 SO3EntityMaterialList,
00559 SO3EntityAttachSkeleton,
00560 SO3EntitySetMaterial,
00561 SO3EntitySetVisibilityFlags,
00562 SO3EntityGetVisibilityFlags
00563 };
00564
00565
00569 int ENTITYnarg[NBENTITYPKG]=
00570 {
00571 1,
00572 2,
00573 1,
00574 2,
00575 3,
00576 2,
00577 1
00578 };
00579
00580
00584 char* ENTITYType[NBENTITYPKG]=
00585 {
00586 "fun [SO3_OBJECT] I",
00587 "fun [SO3_OBJECT I] SO3_MATERIAL",
00588 "fun [SO3_OBJECT] [SO3_MATERIAL r1]",
00589 "fun [SO3_OBJECT P] SO3_OBJECT",
00590 "fun [SO3_OBJECT SO3_MATERIAL I] I",
00591 "fun [SO3_OBJECT I] I",
00592 "fun [SO3_OBJECT] I"
00593 };
00594
00595
00601 int SCOLloadEntity(mmachine m,cbmachine w)
00602 {
00603 return PKhardpak(m, "Entity", NBENTITYPKG, ENTITYname, ENTITYFunc, ENTITYnarg, ENTITYType);
00604 }
00605
00606
00611 int SCOLfreeEntity()
00612 {
00613 return 0;
00614 }