bool OFTexturePaletteRecord::readTexAttr(TexAttr &attr)
{
    bool        returnValue = false;
    PathHandler *ph         = ImageFileHandler::the()->getPathHandler();

    if(ph != NULL)
    {
        std::string imgFile  = szFilename;
        std::string attrFile = imgFile + ".attr";

        attrFile = ph->findFile(attrFile.c_str());

        if(attrFile.empty() == false)
        {
            OSG_OPENFLIGHT_LOG(("OFTexturePaletteRecord::readTexAttr: [%s]\n",
                                attrFile.c_str()));

            std::ifstream ifs;
            ifs.open(attrFile.c_str(), std::ios::in | std::ios::binary);

            Inherited::readVal(ifs, attr.numTexelU);
            Inherited::readVal(ifs, attr.numTexelV);
            Inherited::readVal(ifs, attr.realSizeU);
            Inherited::readVal(ifs, attr.realSizeV);
            Inherited::readVal(ifs, attr.upX);
            Inherited::readVal(ifs, attr.upY);
            Inherited::readVal(ifs, attr.fileFormat);
            Inherited::readVal(ifs, attr.minFilter);
            Inherited::readVal(ifs, attr.magFilter);
            Inherited::readVal(ifs, attr.wrapUV);
            Inherited::readVal(ifs, attr.wrapU);
            Inherited::readVal(ifs, attr.wrapV);
            Inherited::readVal(ifs, attr.modified);
            Inherited::readVal(ifs, attr.pivotX);
            Inherited::readVal(ifs, attr.pivotY);
            Inherited::readVal(ifs, attr.envMode);

            if(attr.wrapU == 3)
                attr.wrapU = attr.wrapUV;
            if(attr.wrapV == 3)
                attr.wrapV = attr.wrapUV;

            returnValue = true;
        }
    }

    return returnValue;
}
OSG_USING_NAMESPACE

// Documentation for this class is emited in the
// OSGProxyGroupBase.cpp file.
// To modify it, please change the .fcd file (OSGProxyGroup.fcd) and
// regenerate the base file.

/*-------------------------------------------------------------------------*/
/*                               Sync                                      */

void ProxyGroup::changed(ConstFieldMaskArg whichField, 
                         UInt32            origin,
                         BitVector         details)
{
    if(whichField & (UrlFieldMask))
    {
        if(getAbsoluteUrl().empty())
        {
            PathHandler *ph = SceneFileHandler::the()->getPathHandler();

            if(ph != NULL) 
            {
                setAbsoluteUrl(ph->findFile(getUrl().c_str()));
            }

            if(getAbsoluteUrl().empty())
            {
                setAbsoluteUrl(getUrl());
            }

            setState(NOT_LOADED);
        }
    }
    if(whichField & (StateFieldMask |
                     UrlFieldMask   |
                     VolumeFieldMask))
    {
        for(UInt32 i = 0; i < _mfParents.size(); i++)
        {
            _mfParents[i]->invalidateVolume();
        }
    }

    Inherited::changed(whichField, origin, details);
}
示例#3
0
Int32 OBJSceneFileType::readMTL ( const Char8 *fileName,
                                  std::map<std::string,
                                  SimpleTexturedMaterialUnrecPtr> & mtlMap )
  const
{
    if(fileName == NULL || strlen(fileName) == 0)
        return 0;

    Int32 mtlCount = 0;

    PathHandler *pathHandler = SceneFileHandler::the()->getPathHandler();
    std::string fullFilePath;

    if(pathHandler != NULL)
        fullFilePath = pathHandler->findFile(fileName);
    else
        fullFilePath = fileName;

    if(fullFilePath.empty())
    {
        FWARNING (("Couldn't open '%s'!\n", fileName));
        return 0;
    }

    std::ifstream in(fullFilePath.c_str());
    SimpleTexturedMaterialUnrecPtr mtlPtr = NULL;
    Real32 a,b,c;
    std::string elem;
    std::map<std::string, MaterialElem>::const_iterator elemI;
    MaterialElem mtlElem;
    std::map<std::string, OSG::ImageUnrecPtr> imageMap;
    std::map<std::string, OSG::ImageUnrecPtr>::iterator iI;
    ImageUnrecPtr image = NULL;
    bool constDiffuse = false, constAmbient = false, constSpecular = false;

    if (in)
    {
        for (in >> elem; in.eof() == false; in >> elem)
        {
            if (elem[0] == '#' || elem[0] == '$' )
            {
                in.ignore(INT_MAX, '\n');
            }
            else
            {
                elemI = _mtlElemMap.find(elem);
                mtlElem = ((elemI == _mtlElemMap.end()) ?
                     UNKNOWN_ME : elemI->second);
                if (mtlElem == NEW_MTL_ME)
                {
                    in >> elem;
                    mtlPtr = SimpleTexturedMaterial::create();
                    mtlPtr->setColorMaterial(GL_NONE);
                    mtlPtr->setEnvMode(GL_MODULATE);
                    mtlMap[elem] = mtlPtr;
                    mtlCount++;
                    constDiffuse  = false;
                    constAmbient  = false;
                    constSpecular = false;
                }
                else
                {
                    if (mtlPtr == NULL)
                    {
                        FFATAL (( "Invalid Mtl token: %s, newmtl expected in %s\n",
                                elem.c_str(), fileName ));
                        in.ignore(INT_MAX, '\n');
                    }
                    else
                    {
                        switch (mtlElem)
                        {
                            case MTL_DIFFUSE_ME:
                                in >> a >> b >> c;
                                if (!constDiffuse)
                                    mtlPtr->setDiffuse( Color3f( a,b,c ));
                            break;
                            case MTL_AMBIENT_ME:
                                in >> a >> b >> c;
                                if (!constAmbient)
                                    mtlPtr->setAmbient( Color3f( a,b,c ));
                            break;
                            case MTL_SPECULAR_ME:
                                in >> a >> b >> c;
                                if (!constSpecular)
                                    mtlPtr->setSpecular( Color3f( a,b,c ));
                            break;
                            case MTL_SHININESS_ME:
                                in >> a;
                                mtlPtr->setShininess(a);
                            break;
                            case MTL_ILLUM_ME:
                                ; // TODO: What to do with illum ?!?
                                in >> elem;
                                // FFATAL (("obj mtl illum not handled yet\n"));
                            break;
                            case MTL_REFL_ME:
                                mtlPtr->setEnvMap(true);
                            break;
                            case MTL_TRANSPARENCY_ME:
                                in >> a;
                                mtlPtr->setTransparency(a);
                            break;
                            case MTL_DISSOLVE_ME:
                                in >> a;
                                mtlPtr->setTransparency(1.f - a);
                                break;
                            case MTL_MAP_KD_ME:
                            case MTL_MAP_KA_ME:
                            case MTL_MAP_KS_ME:
                                image = NULL;
                                in >> elem;
                                iI = imageMap.find(elem);
                                if (iI == imageMap.end())
                                {
                                    std::string fullElemPath;
                                    if(pathHandler != NULL)
                                        fullElemPath = pathHandler->findFile(elem.c_str());
                                    else
                                        fullElemPath = elem.c_str();
                                    image = OSG::ImageFileHandler::the()->read(fullElemPath.c_str());

                                    if(image != NULL)
                                    {
                                        image->setForceAlphaBinary(
                                            image->calcIsAlphaBinary());

                                        imageMap[elem] = image;
                                    }
                                }
                                else
                                {
                                    image = iI->second;
                                }
                                if (image != NULL)
                                {
                                    mtlPtr->setImage(image);
                                    switch (mtlElem)
                                    {
                                        case MTL_MAP_KD_ME:
                                            constDiffuse = true;
                                            mtlPtr->setDiffuse  ( Color3f( 1.f, 1.f, 1.f) );
                                        break;
                                        case MTL_MAP_KA_ME:
                                            constAmbient = true;
                                            mtlPtr->setAmbient  ( Color3f( 1.f, 1.f, 1.f) );
                                        break;
                                        case MTL_MAP_KS_ME:
                                            constSpecular = true;
                                            mtlPtr->setSpecular ( Color3f( 1.f, 1.f, 1.f) );
                                        break;
                                        default:
                                        break;
                                    }
                                }
                                else
                                {
                                    FFATAL (( "Can not find %s texture file in mtl %s \n",
                                            elem.c_str(), fileName ));
                                }
                            break;
                            default:
                                FWARNING (( "Invalid %s entry in %s\n",
                                            elem.c_str(), fileName ));
                                in.ignore(INT_MAX, '\n');
                        }
                    }
                }
            }
        }
示例#4
0
void CharacterModel::convertMaterials(std::string configfile)
{
    getMaterials().clear();
    UInt32 mcnt = 0;
    PathHandler ph;
    
    ph.setBaseFile(configfile.c_str());
       
    for(int mid = 0; mid < _coreModel->getCoreMaterialCount(); mid++)
    {
        CalCoreMaterial *coremat = _coreModel->getCoreMaterial(mid);
        SimpleMaterialPtr mat = SimpleMaterial::create();

        beginEditCP(mat);

        CalCoreMaterial::Color &calamb = coremat->getAmbientColor();
        CalCoreMaterial::Color &caldif = coremat->getDiffuseColor();
        CalCoreMaterial::Color &calspec = coremat->getSpecularColor();

        mat->setAmbient(Color3f(calamb.red / 255.0f, calamb.green / 255.0f, calamb.blue / 255.0f));
        mat->setDiffuse(Color3f(caldif.red / 255.0f, caldif.green / 255.0f, caldif.blue / 255.0f));
        mat->setSpecular(Color3f(calspec.red / 255.0f, calspec.green / 255.0f, calspec.blue / 255.0f));
        
        mat->setShininess(coremat->getShininess() * 100.f);
        mat->setLit(true);
        mat->setColorMaterial(GL_NONE);
        
        for(int mapId = 0; mapId < coremat->getMapCount(); mapId++)
        {
            std::string file = coremat->getMapFilename(mapId);
            std::string pfile = ph.findFile(file.c_str());
            SINFO << "Loading texture '" << pfile << "'..." << endLog;

            ImagePtr img = Image::create();
            
             if(!img->read(pfile.c_str()))
            {
                SWARNING << "CharacterModel::convertMaterials: error "
                         << "loading image " << file << endLog;
            }
            else
            {
                // amz with my test scene paladin.cfg all textures were
                // upside down so I disabled the vertical flipping perhaps
                // they fixed the bug in Cal3D?
#if 0
                beginEditCP(img);
                {
                // For some reason Cal3D expects textures upside down ???
                UInt32 bpl = img->getBpp() * img->getWidth();
                UChar8 *t = img->getData(), 
                       *b = t + (img->getHeight() - 1) * bpl,
                        dum;

                for(UInt32 y = img->getHeight() / 2; y > 0; --y)
                {
                    for(UInt32 x = bpl; x > 0; --x, ++t, ++b)
                    {
                        dum = *t;
                        *t = *b;
                        *b = dum;
                    }
                    b -= bpl * 2;
                }
                }
                endEditCP(img);
#endif
                
                TextureChunkPtr tex = TextureChunk::create();
                
                beginEditCP(tex);
                tex->setImage(img);
                tex->setEnvMode(GL_MODULATE);
                endEditCP(tex);
                
                mat->addChunk(tex);
            }
        }
            
        endEditCP(mat);
        
        coremat->setUserData((Cal::UserData)mcnt);
        getMaterials().push_back(mat);
        mcnt ++;
    }    
}
示例#5
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;
}
示例#6
0
bool DATImageFileType::read(      Image *image,
                            const Char8 *fileName)
{
    bool retCode = false;

    std::ifstream inDat(fileName), inVolS;
    std::istream *inVol;
    std::string keyStr, objectFileName;
    const UInt32 lineBufferSize = 1024;
    Char8 *value, *keySepPos, lineBuffer[lineBufferSize];
    const Char8 keySep = ':';
    int fileOffset, keyL, valueL;
    std::map<std::string, KeyType>::iterator keyI;
    std::map<std::string, FormatDesc>::iterator formatI;
    KeyType key;
    Image::Type formatType;
    UInt32 channel = 1;
    UInt32 res[3];
    UInt32 dataSize = 0;
    Image::PixelFormat pixelFormat = Image::OSG_L_PF;
    char *dataBuffer = 0;
    bool needConversion = false;
    // default endian type is big endian
    bool big_endian = true;

    res[0] = res[1] = res[2] = 0;
    fileOffset = 0;
    formatType = Image::OSG_INVALID_IMAGEDATATYPE;
    dataSize = 0;
    dataBuffer = 0;

    initTypeMap();

    // read the data file
    for(lineBuffer[0] = 0;
        inDat.getline(lineBuffer, lineBufferSize);
        lineBuffer[0] = 0)
    {
        if((keySepPos = strchr(lineBuffer,keySep)))
        {
            keyL = keySepPos - lineBuffer;
            keyStr.assign( lineBuffer, keyL );
            keyI = _keyStrMap.find(keyStr);
            key = ((keyI == _keyStrMap.end()) ? UNKNOWN_KT : keyI->second);
            value = keySepPos + 1;

            while (value && isspace(*value))
                value++;

            valueL = int(strlen(value));

            while (isspace(value[valueL-1]))
                value[--valueL] = 0;

            switch (key)
            {
                case OBJECT_FILE_NAME_KT:
                    objectFileName = value;
                    image->setAttachmentField ( keyStr, value );
                    break;
                case CHANNEL_KT:
                    sscanf ( value, "%u", &(channel) );
                    image->setAttachmentField ( keyStr, value );
                    break;
                case RESOLUTION_KT:
                    sscanf ( value, "%u %u %u",
                             &(res[0]), &(res[1]), &(res[2]));
                    image->setAttachmentField ( keyStr, value );
                    break;
                case FORMAT_KT:
                    formatI = _formatStrMap.find(value);
                    if (formatI != _formatStrMap.end())
                    {
                        formatType = formatI->second.type;
                    }
                    else
                    {
                        formatType = Image::OSG_INVALID_IMAGEDATATYPE;
                    }
                    image->setAttachmentField ( keyStr, value );
                    break;
                case ENDIAN_KT:
                    if(!strcmp(value, "LITTLE"))
                        big_endian = false;
                    image->setAttachmentField ( keyStr, value );
                    break;
                case FILE_OFFSET_KT:
                    sscanf ( value, "%d", &fileOffset );
                    image->setAttachmentField ( keyStr, value );
                    break;
                case UNKNOWN_KT:
                    FNOTICE (( "Uknown DAT file key: >%s<\n",
                                 keyStr.c_str() ));
                    image->setAttachmentField ( keyStr, value );
                    break;
                case SLICE_THICKNESS_KT:
                default:
                    image->setAttachmentField ( keyStr, value );
                    break;
            }
        }
        else
        {
            FINFO (("Skip DAT line\n"));
        }
    }

    // set pixelformat
    switch (channel) 
    {
        case 4:
            pixelFormat = Image::OSG_RGBA_PF;
            break;
        case 3:
            pixelFormat = Image::OSG_RGB_PF;
            break;
        case 2:
            pixelFormat = Image::OSG_LA_PF;
            break;
        default:
            pixelFormat = Image::OSG_L_PF;
            break;
    }

    // check the setting and read the raw vol data
    if (objectFileName.empty() == false)
    {
        if((res[0] > 0) && (res[1] > 0) && (res[2] > 0))
        {
            if(formatType != Image::OSG_INVALID_IMAGEDATATYPE)
            {
                inVolS.open(objectFileName.c_str(),
                            std::ios::in | std::ios::binary);

                if (inVolS.fail() && ImageFileHandler::the()->getPathHandler())
                {
                    // Try to find the file in the search path
                    inVolS.clear(); // reset the error state

                    PathHandler *ph =
                        ImageFileHandler::the()->getPathHandler();

                    inVolS.open(ph->findFile(objectFileName.c_str()).c_str(),
                                std::ios::in | std::ios::binary );
                }

                if(inVolS.fail())
                {
                    // Maybe compressed and name not changed?
                    std::string gzname = objectFileName + ".gz";

                    inVolS.clear(); // reset the error state

                    inVolS.open(gzname.c_str(),
                                std::ios::in | std::ios::binary );

                    if(inVolS.fail() &&
                       ImageFileHandler::the()->getPathHandler())
                    {
                        // Try to find the file in the search path
                        inVolS.clear(); // reset the error state

                        PathHandler *ph =
                            ImageFileHandler::the()->getPathHandler();

                        inVolS.open(ph->findFile(gzname.c_str()).c_str(),
                                    std::ios::in | std::ios::binary );
                    }
                }

                if(inVolS.good())
                {
#ifdef OSG_WITH_ZLIB
                    zip_istream *unzipper = NULL;
#endif

                    image->set(pixelFormat,
                               res[0], res[1], res[2],
                               1, 1, 0.0, 0,
                               formatType);

                    image->clear();

                    dataSize = image->getSize();

                    UInt32 fileDataSize = dataSize;

                    if(isGZip(inVolS))
                    {
#ifdef OSG_WITH_ZLIB
                        unzipper = new zip_istream(inVolS);
                        inVol = unzipper;
#else
                        SFATAL << "Compressed streams are not supported! "
                               << "Configure with --enable-png "
                               << "--with-png=DIR options." << std::endl;
#endif
                    }
                    else
                    {
                        inVol = &inVolS;

                        // get length of the stream.
                        inVol->seekg(0, std::ios::end);
                        UInt64 length = inVol->tellg();
                        inVol->seekg(0, std::ios::beg);

                        if(length < dataSize - fileOffset)
                        {
                            // correct dataSize.
                            fileDataSize = length;
                            FWARNING (( "RAW file length to small!\n" ));
                        }
                        else if(length > dataSize - fileOffset)
                        {
                            FWARNING (( "RAW file length to big!\n" ));
                        }
                    }

                    if(needConversion)
                    {
                        dataBuffer = new char [ dataSize ];
                    }
                    else
                    {
                        dataBuffer = 
                            reinterpret_cast<char *>(image->editData());
                    }

                    if(fileOffset != 0)
                        inVol->ignore (fileOffset);

                    inVol->read ( dataBuffer, fileDataSize );

#ifdef OSG_WITH_ZLIB
                    if(unzipper != NULL)
                        delete unzipper;
#endif
                }
                else
                {
                    FWARNING (( "Can not open %s image data\n",
                                objectFileName.c_str() ));
                }
            }
            else
            {
                FWARNING (( "Invalid/Missing DAT Format\n" ));
            }
        }
        else
        {
            FWARNING (( "Invalid/Missing DAT Resolution\n" ));
        }
    }
    else
    {
        FWARNING (( "Invalid/Missing DAT ObjectFileName\n" ));
    }

    // check/reformat vol data
    if (dataSize && dataBuffer)
    {
        // check host endian type
        UInt16 word = 0x0001;
        UInt8 *byte = reinterpret_cast<UInt8 *>(&word);
        bool host_big_endian = byte[0] ? false : true;

        if(big_endian != host_big_endian)
            image->swapDataEndian();

        if (needConversion)
        {
            FLOG (("DAT-Data convert not impl. yet !\n"));
            {
                switch (formatType)
                {
                    case Image::OSG_UINT8_IMAGEDATA:
                        break;
                    case Image::OSG_UINT16_IMAGEDATA:
                        break;
                    case Image::OSG_UINT32_IMAGEDATA:
                        break;
                    case Image::OSG_FLOAT32_IMAGEDATA:
                        break;
                    default:
                        ;
                }
            }
        }
        else
        {
            retCode = true;
        }
    }


    /* TODO
       std::ifstream in(fileName);
       Head head;
       void *headData = (void*)(&head);
       unsigned dataSize, headSize = sizeof(Head);

       if ( in &&
       in.read(static_cast<char *>(headData),
       headSize) && head.netToHost() &&
       image.set ( Image::PixelFormat(head.pixelFormat),
       head.width, head.height, head.depth, head.mipmapCount,
       head.frameCount, float(head.frameDelay) / 1000.0) &&
       (dataSize = image.getSize()) &&
       in.read((char *)(image.getData()), dataSize ))
       retCode = true;
       else
       retCode = false;
    */

    return retCode;
}