// Pool.cpp #if defined(_MSC_VER) #pragma warning( disable : 4786 ) #endif #include #include #include #include #include #include "MaterialPaletteRecord.h" #include "OldMaterialPaletteRecord.h" #include "Pool.h" #include "Registry.h" #include using namespace flt; osg::Vec4 ColorPool::getColor(int nColorIntensity) { // nColorIntensity: // bit 0-6: intensity // bit 7-15 color index osg::Vec4 col(1,1,1,1); if (nColorIntensity >= 0) { ColorName* cn = getColorName(nColorIntensity >> 7); if (cn) col = cn->getColor(); float intensity = (float)(nColorIntensity & 0x7f)/127.f; col[0] *= intensity; col[1] *= intensity; col[2] *= intensity; } return col; } // getColor for version 11, 12 & 13. osg::Vec4 ColorPool::getOldColor(int nColorIntensity) { // nColorIntensity: // bit 0-6: intensity // bit 7-11 color index // bit 12 fixed intensity bit osg::Vec4 col(1,1,1,1); if (nColorIntensity >= 0) { int nIndex; bool bFixedIntensity = (nColorIntensity & 0x1000) ? true : false; if (bFixedIntensity) nIndex = (nColorIntensity & 0x0fff)+(4096>>7); else nIndex = nColorIntensity >> 7; ColorName* cn = getColorName(nIndex); if (cn) col = cn->getColor(); // intensity if (!bFixedIntensity) { float intensity = (float)(nColorIntensity & 0x7f)/127.f; col[0] *= intensity; col[1] *= intensity; col[2] *= intensity; } } return col; } void ColorPool::addColor(int nIndex, const osg::Vec4& color) { if (nIndex >= 0) { ColorName* colorname = new ColorName; colorname->setColor(color); _colorNameMap[nIndex] = colorname; } } ColorPool::ColorName* ColorPool::getColorName(int nIndex) { ColorNameMap::iterator itr = _colorNameMap.find(nIndex); if (itr != _colorNameMap.end()) return (*itr).second.get(); return NULL; } //////////////////////////////////////////////////////////////////// osg::StateSet* TexturePool::getTexture(int nIndex, int fltVersion) { TexturePaletteMap::iterator fitr = _textureMap.find(nIndex); if (fitr != _textureMap.end()) { return (*fitr).second.get(); } else { // no existing texture state set set up so lets look // for a file name for this nIndex.. TextureNameMap::iterator nitr = _textureNameMap.find(nIndex); if (nitr != _textureNameMap.end()) { const std::string& textureName = (*nitr).second; // Valid index, find the texture // Get StateSet containing texture from registry pool. osg::StateSet* textureStateSet = Registry::instance()->getTexture(textureName); if (textureStateSet) { // Add texture to local pool to be ab121le to get by index. addTexture(nIndex, textureStateSet); } else { CERR<<"setTexture attempting to load ("< image = osgDB::readImageFile(textureName); if (image.valid()) { std::string attrName(textureName); attrName += ".attr"; // Read attribute file char options[256]; sprintf(options,"FLT_VER %d",fltVersion); osgDB::Registry::instance()->setOptions(new osgDB::ReaderWriter::Options(options)); textureStateSet = dynamic_cast(osgDB::readObjectFile(attrName)); osgDB::Registry::instance()->setOptions(NULL); // Delete options // if not found create default StateSet if (textureStateSet == NULL) { textureStateSet = new osg::StateSet; osg::Texture2D* osgTexture = new osg::Texture2D; osgTexture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::REPEAT); osgTexture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::REPEAT); textureStateSet->setTextureAttributeAndModes( unit, osgTexture,osg::StateAttribute::ON); osg::TexEnv* osgTexEnv = new osg::TexEnv; osgTexEnv->setMode(osg::TexEnv::MODULATE); textureStateSet->setTextureAttribute( unit, osgTexEnv ); } osg::Texture2D *osgTexture = dynamic_cast(textureStateSet->getTextureAttribute( unit, osg::StateAttribute::TEXTURE)); if (osgTexture == NULL) { osgTexture = new osg::Texture2D; textureStateSet->setTextureAttributeAndModes( unit, osgTexture,osg::StateAttribute::ON); } osgTexture->setImage(image.get()); } else { // invalid image file, register an empty state set textureStateSet = new osg::StateSet; } // Add new texture to registry pool // ( umm... should this have reference to the texture unit? RO. July2002) Registry::instance()->addTexture(textureName, textureStateSet); // Also add to local pool to be able to get texture by index. // ( umm... should this have reference to the texture unit? RO. July2002) addTexture(nIndex, textureStateSet); CERR<<"Registry::instance()->addTexture("<addTexture("<