Changeset 1285


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

saving and loading entities to file

Location:
GTP/trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/include/TestKdTree.h

    r1274 r1285  
    4646        } 
    4747 
     48        // save entities to ascii file 
     49        bool saveSceneASCII(const String& filename, SceneNode *entityroot); 
    4850protected: 
    4951        KdTreeAppListener *mFrameListener; 
     
    8486        bool loadScene(const String& filename); 
    8587        bool loadSceneIV(const String &filename, SceneNode *root, const int index); 
     88        // loadding world geometry from cfg file - for terrain 
     89        bool loadSceneCfg(const String &filename, SceneNode *root, const int index); 
     90        // load entities from ascii file 
     91        bool loadSceneASCII(const String &filename, SceneNode *root, const int index); 
     92 
     93        typedef std::list<SceneNode *> NodeList; 
     94        void addNodesToList(SceneNode* node, NodeList& list); 
    8695 
    8796        void createMaterials(void); 
  • GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/include/TestKdTreeAppListener.h

    r1274 r1285  
    6666typedef std::list<FrameInfo> FrameList; 
    6767 
     68class KdTreeApp; 
     69 
    6870class KdTreeAppListener: public FrameListener, public KeyListener 
    6971{ 
     
    120122                mDemoMode(false), 
    121123                mEnhancedVisibility(false), 
    122                 mDemoInterval(1.0f) 
     124                mDemoInterval(1.0f), 
     125                myApp(0) 
    123126                { 
    124127 
     
    126129 
    127130                String mSceneFiles; 
     131                String mSceneOutfileName; 
    128132                String mDemoInfileName; 
    129133                String mDemoOutfileName; 
     
    142146                bool mEnhancedVisibility; 
    143147                Real mDemoInterval; 
     148                KdTreeApp *myApp; 
    144149        }; 
    145150 
  • 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/**********************************************************************/ 
  • GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/src/TestKdTreeAppListener.cpp

    r1274 r1285  
    44#include <OgrePanelOverlayElement.h> 
    55#include "TestKdTreeAppListener.h" 
     6#include "TestKdTree.h" 
    67#include <windows.h> 
    78 
     
    543544        } 
    544545 
     546        if (mInputDevice->isKeyDown(KC_F7) && mTimeUntilNextToggle <= 0) 
     547        { 
     548                SceneNode *entnode = mSceneMgr->getSceneNode("AnchorNode"); 
     549                if (!mOptions.myApp->saveSceneASCII(mOptions.mSceneOutfileName, entnode)) 
     550                        LogManager::getSingleton().logMessage( 
     551                        "##Error##: Failed to save scene to " + mOptions.mSceneOutfileName); 
     552                mTimeUntilNextToggle = 0.5; 
     553        } 
     554 
    545555        if (mInputDevice->isKeyDown(KC_F9) && mTimeUntilNextToggle <= 0) 
    546556        { 
  • GTP/trunk/App/Demos/Vis/KdTreeDemo/README.txt

    r1274 r1285  
    4848    Append a comment to the logfile. Requires --logfile to have any effect. 
    4949    (see DEMOS). 
     50     
     51  --savesceneto <filename> 
     52        Specifies file name (with .txt extension) to which the positions of all 
     53        entities in the scene will be saved. If the filename is appended to 
     54        the scene parameter in the config file, the entities are loaded at 
     55        startup 
    5056 
    5157  The following options apply only to the KDT and KTE scene managers. They have 
     
    8894    scene:  Select the scene which shall be loaded. 
    8995            If the parameter is empty or missing, the simple test scene is 
    90             loaded (spaceships & robots). 
    91             If the parameter is "terrain", the application attempts to load 
    92             the terrain world geometry, which only works with scene managers 
    93             supporting this kind of geometry (KTE, OCM, TER). Also a file 
    94             named "terrain.cfg" and holding all the necessary options for 
    95             the terrain must be in the same directory. 
    96             All other values are interpreted as a semicolon-separated list of 
    97             scene files to load. For now, only the .iv format is supported. 
     96            loaded (spaceships & robots on a plane). 
     97            Otherwise the parameter is interpreted as a semicolon-separated 
     98            list of scene files to load. So far the application supports the 
     99            following formats: 
     100                .iv, .wrl:      Mesh files like the vienna scene. 
     101                .cfg:                           Config files for terrain creation. You need to specify 
     102                                                                an appropriate scene manager which supports terrain 
     103                                                                world geometry rendering (KTE, OCM, TER). 
     104                .txt:                           Text file created by the application which store 
     105                                                                positions of entites (small movable objects) 
    98106    movespeed, rotatespeed: 
    99107            Set the speed of movement and rotation for the player camera. 
     
    174182    P     Show/Hide player cam position 
    175183    PRINT Save sceen shot 
     184    F7          Save Entities in the scene to file 
    176185 
    177186DEMOS 
  • GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreKdTreeSceneManager.h

    r1273 r1285  
    6363        /** Overide from scene manager to destroy kdtree properly (before the scene graph is destroyed) 
    6464        */ 
    65         virtual void clearScene() 
    66         { 
    67                 // DEBUG 
    68                 //if (mKdTree) 
    69                 //      mKdTree->dump(); 
    70  
    71                 // must happen before actual scene is cleared 
    72                 OGRE_DELETE(mKdTree); 
    73  
    74                 SceneManager::clearScene(); 
    75         } 
     65        virtual void clearScene(); 
    7666 
    7767        /************************************************************************/ 
  • GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTree.cpp

    r1250 r1285  
    12731273        void KdTree::dump() 
    12741274        { 
    1275                 LogManager::getSingleton().logMessage("#@#@#@#@ Dumping KdTree #@#@#@#@"); 
     1275                //LogManager::getSingleton().logMessage("#@#@#@#@ Dumping KdTree #@#@#@#@"); 
     1276                mBuildLog->logMessage("#@#@#@#@ Dumping KdTree #@#@#@#@"); 
    12761277                if (mKdRoot) 
    12771278                        dump(mKdRoot); 
     
    12811282        void KdTree::dump(KdTree::Node * node) 
    12821283        { 
    1283                 LogManager * log = LogManager::getSingletonPtr(); 
     1284                //LogManager * log = LogManager::getSingletonPtr(); 
    12841285                KdTreeSceneNode * scenenode; 
    12851286                String pad; 
     
    13001301                        { 
    13011302                                scenenode = dynamic_cast<KdTreeSceneNode *>(*it); 
    1302                                 log->logMessage(pad + "# Leaf   level " +  
     1303                                mBuildLog->logMessage(pad + "# Leaf   level " +  
    13031304                                        StringConverter::toString(node->getLevel()) +  
    13041305                                        " SceneNode " + scenenode->getName()); 
    1305                                 log->logMessage(pad + "## Objects: " + scenenode->dumpToString()); 
     1306                                mBuildLog->logMessage(pad + "## Objects: " + scenenode->dumpToString()); 
    13061307                                it++; 
    13071308                        } 
     
    13121313                        if (branch->mLeft) 
    13131314                        { 
    1314                                 log->logMessage(pad + "# Branch level " +  
     1315                                mBuildLog->logMessage(pad + "# Branch level " +  
    13151316                                        StringConverter::toString(node->getLevel()) + " Left Child"); 
    13161317                                dump(branch->mLeft); 
     
    13181319                        if (branch->mRight) 
    13191320                        { 
    1320                                 log->logMessage(pad + "# Branch level " +  
     1321                                mBuildLog->logMessage(pad + "# Branch level " +  
    13211322                                        StringConverter::toString(node->getLevel()) + " Right Child"); 
    13221323                                dump(branch->mRight); 
  • GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTreeSceneManager.cpp

    r1273 r1285  
    6767        delete mKdTree; 
    6868} 
     69 
     70void KdTreeSceneManager::clearScene() 
     71{ 
     72        // DEBUG 
     73        if (mKdTree) 
     74                mKdTree->dump(); 
     75 
     76        // must happen before actual scene is cleared 
     77        OGRE_DELETE(mKdTree); 
     78 
     79        SceneManager::clearScene(); 
     80} 
     81 
    6982 
    7083const String& KdTreeSceneManager::getTypeName(void) const 
Note: See TracChangeset for help on using the changeset viewer.