Exemplo n.º 1
0
bool Cal3DCoreHelper::loadCfg(const std::string &strFilename)
{
    // open the model configuration file
    m_name = strFilename;
#if MAX_PRODUCT_VERSION_MAJOR > 14
    TSTR tmpN;
    tmpN.FromUTF8(m_name.c_str());
    TSTR tmpName = myVRMLName(tmpN);
    m_VrmlName = tmpName.ToCStr().data();
#else
    m_VrmlName = myVRMLName(m_name.c_str());
#endif

    std::ifstream file;
    file.open(strFilename.c_str(), std::ios::in | std::ios::binary);
    if (!file)
    {
        std::cerr << "Failed to open model configuration file '" << strFilename << "'." << std::endl;
        return false;
    }
    size_t pathend = strFilename.find_last_of("/\\");
    if (pathend != std::string::npos)
    {
        m_path = strFilename.substr(0, pathend + 1);
    }

    // initialize the data path
    std::string strPath = m_path;

    // initialize the animation count
    m_animationCount = 0;

    // 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 happened while reading from the file
        if (!file)
        {
            std::cerr << "Error while reading from the model configuration file '" << strFilename << "'." << std::endl;
            return false;
        }

        // 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] != '='))
        {
            std::cerr << strFilename << "(" << line << "): Invalid syntax." << std::endl;
            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_renderScale = (float)atof(strData.c_str());
        }
        else if (strKey == "path")
        {
            // set the new path for the data files if one hasn't been set already
            if (m_path == "")
                strPath = strData;
        }
        else if (strKey == "skeleton")
        {
            // load core skeleton
            std::cout << "Loading skeleton '" << strData << "'..." << std::endl;
            if (!m_calCoreModel->loadCoreSkeleton(strPath + strData))
            {
                CalError::printLastError();
                return false;
            }
        }
        else if (strKey == "animation")
        {
            // load core animation
            std::cout << "Loading animation '" << strData << "'..." << std::endl;
            m_animationId[m_animationCount] = m_calCoreModel->loadCoreAnimation(strPath + strData);
            if (m_animationId[m_animationCount] == -1)
            {
                CalError::printLastError();
                return false;
            }

            m_animationCount++;
        }
        else if (strKey == "mesh")
        {
            // load core mesh
            std::cout << "Loading mesh '" << strData << "'..." << std::endl;
            if (m_calCoreModel->loadCoreMesh(strPath + strData) == -1)
            {
                CalError::printLastError();
                return false;
            }
        }
        else if (strKey == "material")
        {
            // load core material
            std::cout << "Loading material '" << strData << "'..." << std::endl;
            if (m_calCoreModel->loadCoreMaterial(strPath + strData) == -1)
            {
                CalError::printLastError();
                return false;
            }
        }
        else
        {
            std::cerr << strFilename << "(" << line << "): Invalid syntax." << std::endl;
            return false;
        }
    }

    // explicitely close the file
    file.close();

    // load all textures and store the opengl texture id in the corresponding map in the material
    int materialId;
    for (materialId = 0; materialId < m_calCoreModel->getCoreMaterialCount(); materialId++)
    {
        // get the core material
        CalCoreMaterial *pCoreMaterial;
        pCoreMaterial = m_calCoreModel->getCoreMaterial(materialId);

        // loop through all maps of the core material
        int mapId;
        for (mapId = 0; mapId < pCoreMaterial->getMapCount(); mapId++)
        {
            // get the filename of the texture
            std::string strFilename;
            strFilename = pCoreMaterial->getMapFilename(mapId);

            // load the texture from the file
            // GLuint textureId;
            // textureId = loadTexture(strPath + strFilename);

            // store the opengl texture id in the user data of the map
            // pCoreMaterial->setMapUserData(mapId, (Cal::UserData)textureId);
        }
    }

    // make one material thread for each material
    // NOTE: this is not the right way to do it, but this viewer can't do the right
    // mapping without further information on the model etc.
    for (materialId = 0; materialId < m_calCoreModel->getCoreMaterialCount(); materialId++)
    {
        // create the a material thread
        m_calCoreModel->createCoreMaterialThread(materialId);

        // initialize the material thread
        m_calCoreModel->setCoreMaterialId(materialId, 0, materialId);
    }

    // Calculate Bounding Boxes

    m_calCoreModel->getCoreSkeleton()->calculateBoundingBoxes(m_calCoreModel);

    m_calModel = new CalModel(m_calCoreModel);

    // attach all meshes to the model
    int meshId;
    for (meshId = 0; meshId < m_calCoreModel->getCoreMeshCount(); meshId++)
    {
        m_calModel->attachMesh(meshId);
    }

    // set the material set of the whole model
    m_calModel->setMaterialSet(0);

    // set initial animation state
    m_state = 0;
    m_calModel->getMixer()->blendCycle(m_animationId[0], 1.0, 0.0f);

    return true;
}