Esempio n. 1
0
CalCoreModel *CharacterModel::loadConfig(std::string filename)
{
    PathHandler ph;
    std::string pfile;
    
    ph.setBaseFile(filename.c_str());
    
    // open the model configuration file
    std::ifstream file;
    file.open(filename.c_str(), std::ios::in | std::ios::binary);
    if(!file)
    {
        SWARNING << "Failed to open model configuration file '" 
                 << filename << "'." << endLog;
        return NULL;
    }

    CalCoreModel *model = new CalCoreModel("dummy");
    
    // parse all lines from the model configuration file
    int line;
    for(line = 1; ; line++)
    {
        // read the next model configuration line
        std::string strBuffer;
        std::getline(file, strBuffer);

        // stop if we reached the end of file
        if(file.eof()) break;

        // check if an error happend while reading from the file
        if(!file)
        {
            SWARNING << "Error while reading from the model configuration file '" 
                     << filename << "'." << endLog;
            return NULL;
        }

        // find the first non-whitespace character
        std::string::size_type pos;
        pos = strBuffer.find_first_not_of(" \t");

        // check for empty lines
        if((pos == std::string::npos) || (strBuffer[pos] == '\n') || 
           (strBuffer[pos] == '\r') || (strBuffer[pos] == 0)) 
            continue;

        // check for comment lines
        if(strBuffer[pos] == '#') continue;

        // get the key
        std::string strKey;
        strKey = strBuffer.substr(pos, strBuffer.find_first_of(" =\t\n\r", pos) - pos);
        pos += strKey.size();

        // get the '=' character
        pos = strBuffer.find_first_not_of(" \t", pos);
        if((pos == std::string::npos) || (strBuffer[pos] != '='))
        {
            SWARNING << filename << "(" << line << "): Invalid syntax." 
                     << endLog;
            return false;
        }

        // find the first non-whitespace character after the '=' character
        pos = strBuffer.find_first_not_of(" \t", pos + 1);

        // get the data
        std::string strData;
        strData = strBuffer.substr(pos, 
                            strBuffer.find_first_of("\n\r", pos) - pos);

        // handle the model creation
        if(strKey == "scale")
        {
            // set rendering scale factor
            //m_scale = atof(strData.c_str());
        }
        else if(strKey == "skeleton")
        {
            pfile = ph.findFile(strData.c_str());

            // load core skeleton
            SINFO << "Loading skeleton '" << pfile << "'..." << endLog;
            
            if(!model->loadCoreSkeleton(pfile.c_str()))
            {
                CalError::printLastError();
                return false;
            }
        }
        else if(strKey == "animation")
        {
            pfile = ph.findFile(strData.c_str());
            
            // load core animation
            SINFO << "Loading animation '" << pfile 
                  << "'..." << endLog;
            if(model->loadCoreAnimation(pfile.c_str()) == -1)
            {
                CalError::printLastError();
                return false;
            }
        }
        else if(strKey == "mesh")
        {
            pfile = ph.findFile(strData.c_str());

            // load core mesh
            SINFO << "Loading mesh '" << pfile << "'..." << endLog;
            if(model->loadCoreMesh(pfile.c_str()) == -1)
            {
                CalError::printLastError();
                return false;
            }
        }
        else if(strKey == "material")
        {
            pfile = ph.findFile(strData.c_str());

            // load core material
            SINFO << "Loading material '" << pfile << "'..." << endLog;
            if(model->loadCoreMaterial(pfile.c_str()) == -1)
            {
                CalError::printLastError();
                return false;
            }
        }
        else
        {
            // everything else triggers an error message, but is ignored
            SWARNING << filename << "(" << line << "): Invalid syntax." 
                     << endLog;
        }
    }

    // create material threads
    int mid;
    for(mid = 0; mid < model->getCoreMaterialCount(); mid++)
    {
        model->createCoreMaterialThread(mid);
        model->setCoreMaterialId(mid, 0, mid);
    }

    file.close();

    return model;
}
Esempio n. 2
0
void Cal3dCore::loadCore(const char *fn)
{
    coreModel = new osgCal::CoreModel();
    animNum = -1;
    meshAdder = new osgCal::DefaultMeshAdder;
    p = new osgCal::MeshParameters;
    p->useDepthFirstMesh = false;
    p->software = false; // default

    try
    {
        std::string ext = osgDB::getLowerCaseFileExtension(fn);
        std::string dir = osgDB::getFilePath(fn);
        std::string name = osgDB::getStrippedName(fn);

        if (dir == "")
        {
            dir = ".";
        }

        if (ext == "caf")
        {
            coreModel->load(dir + "/cal3d.cfg", p.get());

            for (size_t i = 0; i < coreModel->getAnimationNames().size(); i++)
            {
                if (coreModel->getAnimationNames()[i] == name)
                {
                    animNum = i;
                    break;
                }
            }

            if (animNum == -1)
            {
                // animation is absent in cal3d.cfg, so load it manually
                CalCoreModel *cm = coreModel->getCalCoreModel();

                std::cout << coreModel->getScale() << std::endl;
                if (coreModel->getScale() != 1)
                {
                    // to eliminate scaling of the model by non-scaled animation
                    // we scale model back, load animation, and rescale one more time
                    cm->scale(1.0 / coreModel->getScale());
                }

                animNum = cm->loadCoreAnimation(fn);

                if (coreModel->getScale() != 1)
                {
                    cm->scale(coreModel->getScale());
                }
            }
        }
        else if (ext == "cmf")
        {
            coreModel->load(dir + "/cal3d.cfg", p.get());
            meshAdder = new osgCal::OneMeshAdder(osgDB::getStrippedName(fn));
        }
        else
        {
            coreModel->load(fn, p.get());
        }
    }
    catch (std::runtime_error &e)
    {
        std::cout << "runtime error during load:" << std::endl
                  << e.what() << std::endl;
        return;
    }
}