示例#1
0
//-----------------------------------------------------------------------------
int COFSSceneSerializer::_writeFile(Ogre::String exportfile, const bool forceSave)
{
    if (exportfile.empty())
        return SCF_ERRUNKNOWN;

    OgitorsRoot *ogRoot = OgitorsRoot::getSingletonPtr();
    // Open a stream to output our XML Content and write the general headercopyFile
    std::stringstream outfile;

    outfile << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    outfile << "<OGITORSCENE version=\"" << Globals::OGSCENE_FORMAT_VERSION << "\">\n";

    PROJECTOPTIONS *pOpt = ogRoot->GetProjectOptions();
    pOpt->CameraSpeed = ogRoot->GetViewport()->GetCameraSpeed();

    ogRoot->WriteProjectOptions(outfile, pOpt);

    ObjectVector ObjectList;
    OgitorsPropertyValueMap theList;
    OgitorsPropertyValueMap::iterator ni;

    // Start from 1, since 0 means all objects
    for(unsigned int i = 1; i < LAST_EDITOR; i++)
    {
        ogRoot->GetObjectList(i, ObjectList);
        for(unsigned int ob = 0; ob < ObjectList.size(); ob++)
        {
            /// If Object does not have a parent, then it is not part of the scene
            if(ObjectList[ob]->getParent())
            {
                ObjectList[ob]->onSave(forceSave);
                if(ObjectList[ob]->isSerializable())
                {
                    outfile << OgitorsUtils::GetObjectSaveStringV2(ObjectList[ob], 2, true, true).c_str();
                    outfile << "\n";
                }
            }
        }
    }
    outfile << "</OGITORSCENE>\n";

    if (OgitorsUtils::SaveStreamOfs(outfile, exportfile)) {
        return SCF_OK;
    }

    return SCF_ERRFILE;
}
//----------------------------------------------------------------------------
int CDotSceneSerializer::Import(Ogre::String importfile)
{
    OgitorsRoot *ogRoot = OgitorsRoot::getSingletonPtr();
    OgitorsSystem *mSystem = OgitorsSystem::getSingletonPtr();

    if(importfile == "")
    {
        UTFStringVector extlist;
        extlist.push_back(OTR("DotScene File"));
        extlist.push_back("*.scene");
        extlist.push_back(OTR("DotScene File"));
        extlist.push_back("*.xml");
        importfile = mSystem->DisplayOpenDialog(OTR("Import DotScene File"),extlist);
        if(importfile == "") return SCF_CANCEL;
    }

    ogRoot->ClearProjectOptions();

    Ogre::String filePath = OgitorsUtils::ExtractFilePath(importfile);
    Ogre::String fileName = OgitorsUtils::ExtractFileName(importfile);

    PROJECTOPTIONS *pOpt = ogRoot->GetProjectOptions();
    pOpt->CreatedIn = "";

    if(filePath.find(".") == 0)
    {
        filePath = OgitorsUtils::GetExePath() + filePath;
        filePath = OgitorsUtils::QualifyPath(filePath);
    }

    pOpt->ProjectDir = filePath;
    int typepos = fileName.find_last_of(".");
    pOpt->ProjectName = fileName;
    if(typepos != -1)
        pOpt->ProjectName.erase(typepos,pOpt->ProjectName.length() - typepos);

    TiXmlDocument docImport((filePath + fileName).c_str());

    if(!docImport.LoadFile()) return SCF_ERRFILE;

    TiXmlElement* element = 0;
    element = docImport.FirstChildElement("scene");
    if(!element)
        return SCF_ERRFILE;

    float version = Ogre::StringConverter::parseReal(ValidAttr(element->Attribute("formatVersion")));
    if(version != 1.0f)
    {
        mSystem->DisplayMessageDialog(OTR("Only File Version 1.0 is supported!"),DLGTYPE_OK);
        return SCF_ERRFILE;
    }
    
    pOpt->SceneManagerName = "OctreeSceneManager";

    pOpt->ResourceDirectories.push_back("/");
    TiXmlElement* resLoc = element->FirstChildElement("resourceLocations");
    if(resLoc)
    {
        resLoc = resLoc->FirstChildElement();
        while(resLoc)
        {
            Ogre::String resType = ValidAttr(resLoc->Attribute("type"));
            Ogre::String resName = ValidAttr(resLoc->Attribute("name"));
            if(resType == "FileSystem")
            {
                OgitorsUtils::CleanPath(resName);
                
                if(resName[0] == '.')
                    resName.erase(0, 1);

                pOpt->ResourceDirectories.push_back(resName);
            }

            resLoc = resLoc->NextSiblingElement();
        }
    }

    TiXmlElement* configData = element->FirstChildElement("terrain");
    if(configData)
    {
        pOpt->SceneManagerConfigFile = ValidAttr(configData->Attribute("dataFile"));
    }

    pOpt->CameraPositions[0] = Ogre::Vector3(0,10,0);
    pOpt->CameraOrientations[0] = Ogre::Quaternion::IDENTITY;
    pOpt->CameraPositions[1] = Ogre::Vector3(0,10,0);
    pOpt->CameraOrientations[1] = Ogre::Quaternion::IDENTITY;
    pOpt->CameraSaveCount = 1;

    OFS::OfsPtr& ofsFile = OgitorsRoot::getSingletonPtr()->GetProjectFile();

    Ogre::String ofs_file_name = OgitorsUtils::QualifyPath(filePath + "/" + pOpt->ProjectName + ".ofs");
    
    if(ofsFile.mount(ofs_file_name.c_str(), OFS::OFS_MOUNT_CREATE) != OFS::OFS_OK)
        return SCF_ERRFILE;

    OgitorsUtils::CopyDirOfs(filePath, "/");

    ofsFile->deleteFile(fileName.c_str());

    ofs_file_name = OgitorsUtils::ExtractFileName(ofs_file_name);

    ofsFile->deleteFile(ofs_file_name.c_str());
    
    ogRoot->PrepareProjectResources();

    OgitorsPropertyValueMap params;
    OgitorsPropertyValue propValue;
    
    propValue.propType = PROP_STRING;
    propValue.val = Ogre::Any(pOpt->SceneManagerConfigFile);
    params["configfile"] = propValue;
        
    Ogre::Vector2 vClipping(1,1000);
    TiXmlElement* environment = element->FirstChildElement("environment");
    if(environment)
    {
        TiXmlElement* current = environment->FirstChildElement("clipping");
        if(current)
        {
            vClipping.x = Ogre::StringConverter::parseReal(ValidAttr(current->Attribute("near"),"1"));
            vClipping.y = Ogre::StringConverter::parseReal(ValidAttr(current->Attribute("far"),"1000"));
        }

        current = environment->FirstChildElement("colourAmbient");
        if(current)
        {
            params["ambient"] = parseColourValue(current);
        }
        
        current = environment->FirstChildElement("skyBox");
        if(current)
        {
            propValue.propType = PROP_BOOL;
            propValue.val = Ogre::Any(Ogre::StringConverter::parseBool(ValidAttr(current->Attribute("enable"),"0")));
            params["skybox::active"] = propValue;
            propValue.propType = PROP_STRING;
            propValue.val = Ogre::Any(Ogre::String(ValidAttr(current->Attribute("material"))));
            params["skybox::material"] = propValue;
            propValue.propType = PROP_REAL;
            propValue.val = Ogre::Any(Ogre::StringConverter::parseReal(ValidAttr(current->Attribute("distance"),"0")));
            params["skybox::distance"] = propValue;
        }

        current = environment->FirstChildElement("skyDome");
        if(current)
        {
            propValue.propType = PROP_BOOL;
            propValue.val = Ogre::Any(Ogre::StringConverter::parseBool(ValidAttr(current->Attribute("enable"),"0")));
            params["skydome::active"] = propValue;
            propValue.propType = PROP_STRING;
            propValue.val = Ogre::Any(Ogre::String(ValidAttr(current->Attribute("material"))));
            params["skydome::material"] = propValue;
        }

        current = environment->FirstChildElement("fog");
        if(current)
        {
            propValue.propType = PROP_INT;
            Ogre::String fogmode = ValidAttr(current->Attribute("mode"),"None");
            if(fogmode == "Linear" || fogmode == "linear" )
                propValue.val = Ogre::Any((int)Ogre::FOG_LINEAR);
            else if(fogmode == "Exp" || fogmode == "exp" )
                propValue.val = Ogre::Any((int)Ogre::FOG_EXP);
            else if(fogmode == "Exp2" || fogmode == "exp2" )
                propValue.val = Ogre::Any((int)Ogre::FOG_EXP2);
            else
                propValue.val = Ogre::Any(Ogre::StringConverter::parseInt(fogmode));

            params["fog::mode"] = propValue;
            Ogre::Real start = Ogre::StringConverter::parseReal(ValidAttr(current->Attribute("linearStart"),"0"));
            Ogre::Real end = Ogre::StringConverter::parseReal(ValidAttr(current->Attribute("linearEnd"),"1"));

            propValue.propType = PROP_REAL;
            propValue.val = Ogre::Any((Ogre::Real)(start * (vClipping.y - vClipping.x)));
            params["fog::start"] = propValue;
            propValue.val = Ogre::Any((Ogre::Real)(end * (vClipping.y - vClipping.x)));
            params["fog::end"] = propValue;
            propValue.val = Ogre::Any(Ogre::StringConverter::parseReal(ValidAttr(current->Attribute("expDensity"),"0")));
            params["fog::density"] = propValue;
            current = current->FirstChildElement("colourDiffuse");
            if(current)
            {
                params["fogcolour"] = parseColourValue(current);
            }
        }
    }

    propValue.propType = PROP_STRING;
    propValue.val = Ogre::Any(Ogre::String("SceneManager1"));
    params["name"] = propValue;
    CSceneManagerEditor *mngred = static_cast<CSceneManagerEditor*>(ogRoot->CreateEditorObject(0, pOpt->SceneManagerName,params,false,false));

    // read cameras placed outside nodes
    TiXmlElement* otherElems = element->FirstChildElement("camera");
    Ogitors::CBaseEditor* cbeTemp;
    while(otherElems){
        ReadCamera(otherElems, mngred, &cbeTemp);
        otherElems = otherElems->NextSiblingElement("camera");
    }

    // read lights placed outside nodes
    otherElems = element->FirstChildElement("light");
    while(otherElems){
        ReadLight(otherElems, mngred, &cbeTemp);
        otherElems = otherElems->NextSiblingElement("light");
    }

    element = element->FirstChildElement("nodes");

    RecurseReadObjects(element, mngred);
    ogRoot->AfterLoadScene();
    ogRoot->GetViewport()->getCameraEditor()->setClipDistance(vClipping);
    return SCF_OK;
}