Ignore:
Timestamp:
08/25/06 17:44:02 (18 years ago)
Author:
szydlowski
Message:

saving and loading entities to file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/src/TestKdTree.cpp

    r1274 r1285  
    66#include "TestKdTree.h" 
    77#include "WinCmdLineParser.h" 
     8#include <cstring> 
    89 
    910#ifdef __cplusplus 
     
    3738                                .addOpt("s","scenemgr", ARGUMENT_REQUIRED) 
    3839                                .addOpt("","comment", ARGUMENT_REQUIRED) 
    39                                 .addOpt("e","enhancevis", ARGUMENT_NONE); 
     40                                .addOpt("e","enhancevis", ARGUMENT_NONE) 
     41                                .addOpt("","savesceneto", ARGUMENT_REQUIRED); 
    4042 
    4143 
     
    4648                        options.mDemoMode = cmdparser.getOpt("d"); 
    4749                        options.mEnhancedVisibility = cmdparser.getOpt("e"); 
     50                        cmdparser.getOpt("savesceneto", options.mSceneOutfileName); 
    4851 
    4952                        std::string tmp; 
     
    302305                mCamNode->setPosition(707,2500,528); 
    303306                mCamNode->setOrientation(Quaternion(-0.3486, 0.0122, 0.9365, 0.0329)); 
    304                 //mCamNode->setPosition(0,0,0); 
    305                 //mCamNode->setOrientation(Quaternion(Radian(Math::PI),Vector3::UNIT_Y)); 
     307                //mCamNode->setPosition(1056.59, 467.412, 783.502); 
     308                //mCamNode->setOrientation(Quaternion(-0.281354, 0.0204027, 0.956747, 0.0695345)); 
    306309        } 
    307310        // load scene from file 
     
    310313                // don't load the other stuff 
    311314                loadScene(mOptions.mSceneFiles); 
     315 
    312316                // top view 
    313317                mTopCam->setPosition(Vector3(1232, 3990, -1477)); 
     
    531535void KdTreeApp::createFrameListener(void) 
    532536{ 
     537        // set pointer to self in options 
     538        mOptions.myApp = this; 
     539 
    533540        mFrameListener = new KdTreeAppListener(mWindow, mSceneMgr, mOptions); 
    534541        //mFrameListener->showDebugOverlay( true ); 
     
    760767        LogManager::getSingleton().logMessage(d.str()); 
    761768 
    762         bool result = false; 
     769        bool loadres = false; 
     770        bool result = filenames.empty() ? false : true; 
     771 
    763772        std::vector<std::string>::const_iterator fit, fit_end = filenames.end(); 
    764773        int i = 0; 
     
    770779                if (strstr(fn.c_str(), ".iv") || strstr(fn.c_str(), ".wrl")) 
    771780                { 
    772                         // hack: set postion manually for vienna 
    773                         //mCamNode->setPosition(Vector3(830, 300, -540)); 
    774                         //mCamNode->setOrientation(Quaternion(-0.3486, 0.0122, 0.9365, 0.0329)); 
    775  
    776781                        // load iv files 
    777                         loadSceneIV(fn, mSceneMgr->getRootSceneNode(), i);                       
    778                 } 
    779  
    780                 result = true; 
    781         } 
    782  
    783  
    784         /* 
    785         if (result)  
    786         { 
    787         int intersectables, faces; 
    788  
    789         std::stringstream d; 
    790         d << filename << " parsed successfully.\n" 
    791         << "#NUM_OBJECTS (Total numner of objects)\n" << intersectables << "\n" 
    792         << "#NUM_FACES (Total numner of faces)\n" << faces << "\n"; 
    793         } 
    794         */ 
     782                        loadres = loadSceneIV(fn, mSceneMgr->getRootSceneNode(), i);                     
     783                } 
     784                else if (strstr(fn.c_str(), ".cfg")) 
     785                { 
     786                        // load terrain from cfg 
     787                        loadres = loadSceneCfg(fn, mSceneMgr->getRootSceneNode(), i); 
     788                } 
     789                else if (strstr(fn.c_str(), ".txt")) 
     790                { 
     791                        // load entities form text file 
     792                        loadres = loadSceneASCII(fn, mSceneMgr->getRootSceneNode(), i); 
     793                } 
     794                else 
     795                { 
     796                        loadres = false; 
     797                } 
     798 
     799                // result is true only if all files have been succesfully loaded 
     800                result = result ? loadres : false; 
     801        } 
    795802 
    796803        return result; 
     
    802809        IVReader * mIVReader = new IVReader(); 
    803810 
    804         if (1) 
     811        if (0) 
    805812        { 
    806813                String logFilename = "IVLog" + Ogre::StringConverter().toString(index) + ".log"; 
     
    823830                return true; 
    824831        } 
    825  
    826         return false; 
    827 } 
     832        else 
     833        { 
     834                OGRE_DELETE(mIVReader); 
     835 
     836                return false; 
     837        } 
     838} 
     839 
     840//----------------------------------------------------------------------- 
     841bool KdTreeApp::loadSceneCfg(const String &filename, SceneNode *root, const int index) 
     842{ 
     843        // Set ambient light 
     844        mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5)); 
     845        // Create a light 
     846        Light* l = mSceneMgr->createLight("MainLight"); 
     847        l->setPosition(20,80,50); 
     848         
     849        // try to load world geometry defined in the cfg file 
     850        mSceneMgr -> setWorldGeometry( filename ); 
     851 
     852        // return true since we have no feedback from setWorldGeometry 
     853        return true; 
     854} 
     855 
     856#define MAX_BUF_SIZE 1024 
     857 
     858//----------------------------------------------------------------------- 
     859bool KdTreeApp::loadSceneASCII(const String &filename, SceneNode *root, const int index) 
     860{ 
     861        typedef std::map<std::string, std::string> EntMeshMap; 
     862        typedef std::vector<std::string> Line; 
     863         
     864        std::ifstream read; 
     865        read.open(filename.c_str()); 
     866        if (read.is_open()) 
     867        { 
     868                char buf[MAX_BUF_SIZE]; 
     869                Line line; 
     870                std::stringstream s; 
     871                std::string node, ent, mesh; 
     872                Vector3 pos; 
     873                Quaternion or; 
     874                Vector3 scale; 
     875                int count; 
     876                EntMeshMap emm; 
     877 
     878                // the node which is parent to all entities 
     879                SceneNode *anchor = mSceneMgr->getRootSceneNode()->createChildSceneNode("AnchorNode"); 
     880 
     881                while (!read.eof()) 
     882                { 
     883                        line.clear(); 
     884                        memset(buf, 0, MAX_BUF_SIZE); 
     885                        read.getline(buf, MAX_BUF_SIZE); 
     886                        splitFilenames(std::string(buf), line); 
     887                        if (line.size() < 5) 
     888                                continue; 
     889                        // see if node not empty 
     890                        s << line[4]; 
     891                        s >> count; 
     892                        s.clear(); 
     893                        if (count > 0) 
     894                        { 
     895                                // read info 
     896                                node = line[0]; 
     897                                s << line[1]; 
     898                                s >> pos.x >> pos.y >> pos.z; 
     899                                s.clear(); 
     900                                s << line[2]; 
     901                                s >> or.x >> or.y >> or.z >> or.w; 
     902                                s.clear(); 
     903                                s << line[3]; 
     904                                s >> scale.x >> scale.y >> scale.z; 
     905                                s.clear(); 
     906                                for (int i = 5; i < 5 + count; i ++) 
     907                                { 
     908                                        s << line[i]; 
     909                                        s >> ent >> mesh; 
     910                                        s.clear(); 
     911                                        emm[ent] = mesh; 
     912                                } 
     913                                // build node 
     914                                SceneNode *newnode = anchor->createChildSceneNode(node, pos, or); 
     915                                newnode->setScale(scale); 
     916                                for (EntMeshMap::iterator emmit = emm.begin(); emmit != emm.end(); emmit ++) 
     917                                { 
     918                                        Entity * entity = mSceneMgr->createEntity(emmit->first, emmit->second); 
     919                                        newnode->attachObject(entity); 
     920                                } 
     921                                emm.clear(); 
     922                        } 
     923 
     924 
     925                } 
     926 
     927                return true; 
     928        } 
     929        else 
     930        { 
     931                return false; 
     932        } 
     933} 
     934 
     935//----------------------------------------------------------------------- 
     936bool KdTreeApp::saveSceneASCII(const String& filename, SceneNode *entityroot) 
     937{ 
     938        // find all entities under node entityroot and store their name, position 
     939        //and orientation to a test file 
     940        std::ofstream write; 
     941        write.open(filename.c_str()); 
     942        if (write.is_open()) 
     943        { 
     944                NodeList list; 
     945                addNodesToList(entityroot, list); 
     946 
     947                std::string fs = ";", rs = "\n"; 
     948                std::string entnames; 
     949                int entcount; 
     950 
     951                for (NodeList::iterator it = list.begin(); it != list.end(); it ++) 
     952                { 
     953                        // fist dump info about node (name, position, orientation, scale) 
     954                        SceneNode * node = *it; 
     955                        write << node->getName() << fs <<  
     956                                StringConverter::toString(node->getPosition()) << fs << 
     957                                StringConverter::toString(node->getOrientation()) << fs << 
     958                                StringConverter::toString(node->getScale()) << fs; 
     959 
     960                        entcount = 0; 
     961                        entnames = ""; 
     962 
     963                        SceneNode::ObjectIterator objIt = node->getAttachedObjectIterator(); 
     964 
     965                        while (objIt.hasMoreElements()) 
     966                        { 
     967                                MovableObject *movable = objIt.getNext(); 
     968                                if (movable->getMovableType() == "Entity") 
     969                                { 
     970                                        Entity *ent = static_cast<Entity *>(movable); 
     971                                        entcount++; 
     972                                        entnames += fs + ent->getName() + " " + ent->getMesh()->getName(); 
     973 
     974                                } 
     975                        } 
     976 
     977                        // now dump entity count and names 
     978                        write << entcount << entnames << rs; 
     979                } 
     980 
     981                write.close(); 
     982                return true; 
     983        } 
     984        else 
     985        { 
     986                return false; 
     987        } 
     988 
     989} 
     990 
     991void KdTreeApp::addNodesToList(SceneNode* node, NodeList& list) 
     992{ 
     993        //// check if node has entities 
     994        //bool entfound = false; 
     995 
     996        //SceneNode::ObjectIterator objIt = node->getAttachedObjectIterator(); 
     997        //while (objIt.hasMoreElements()) 
     998        //{ 
     999        //      MovableObject *movable = objIt.getNext(); 
     1000        //      if (movable->getMovableType() == "Entity") 
     1001        //              entfound = true; 
     1002        //} 
     1003 
     1004        //// if yes, add to list 
     1005        //if (entfound) 
     1006        list.push_back(node); 
     1007 
     1008        // check if node has children and add them to list 
     1009        SceneNode::ChildNodeIterator childIt = node->getChildIterator(); 
     1010        while (childIt.hasMoreElements()) 
     1011        { 
     1012                SceneNode *child = static_cast<SceneNode *>(childIt.getNext()); 
     1013                addNodesToList(child, list); 
     1014        } 
     1015} 
     1016 
    8281017 
    8291018/**********************************************************************/ 
Note: See TracChangeset for help on using the changeset viewer.