예제 #1
0
static void load_material_texture( obj::Model &model,
                            obj::Material &material,
                            osg::StateSet *stateset,
                            const std::string & filename,
                            const unsigned int texture_unit  )
{
    if (!filename.empty())
    {
        osg::ref_ptr< osg::Image > image;
        if ( !model.getDatabasePath().empty() ) 
        {
            // first try with database path of parent. 
            image = osgDB::readImageFile(model.getDatabasePath()+'/'+filename);
        }
        
        if ( !image.valid() )
        {
            // if not already set then try the filename as is.
            image = osgDB::readImageFile(filename);
        }

        if ( image.valid() )
        {
            osg::Texture2D* texture = new osg::Texture2D( image.get() );
            osg::Texture::WrapMode textureWrapMode = osg::Texture::REPEAT;
            texture->setWrap(osg::Texture2D::WRAP_R, textureWrapMode);
            texture->setWrap(osg::Texture2D::WRAP_S, textureWrapMode);
            texture->setWrap(osg::Texture2D::WRAP_T, textureWrapMode);
            stateset->setTextureAttributeAndModes( texture_unit, texture,osg::StateAttribute::ON );
            
            if ( material.textureReflection )
            {
                osg::TexGen* texgen = new osg::TexGen;
                texgen->setMode(osg::TexGen::SPHERE_MAP);
                stateset->setTextureAttributeAndModes( texture_unit,texgen,osg::StateAttribute::ON );
            }
            
            if  ( image->isImageTranslucent())
            {
                osg::notify(osg::INFO)<<"Found transparent image"<<std::endl;
                stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
                stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
            }
        }
    }

    if (material.uScale != 1.0f || material.vScale != 1.0f ||
            material.uOffset != 0.0f || material.vOffset != 0.0f)
    {
        osg::Matrix mat;
        if (material.uScale != 1.0f || material.vScale != 1.0f)
        {
            osg::notify(osg::DEBUG_INFO) << "Obj TexMat scale=" << material.uScale << "," << material.vScale << std::endl;
            mat *= osg::Matrix::scale(material.uScale, material.vScale, 1.0);
        }
        if (material.uOffset != 0.0f || material.vOffset != 0.0f)
        {
            osg::notify(osg::DEBUG_INFO) << "Obj TexMat offset=" << material.uOffset << "," << material.uOffset << std::endl;
            mat *= osg::Matrix::translate(material.uOffset, material.vOffset, 0.0);
        }

        osg::TexMat* texmat = new osg::TexMat;
        texmat->setMatrix(mat);
        stateset->setTextureAttributeAndModes( texture_unit,texmat,osg::StateAttribute::ON );
    }
}
예제 #2
0
static void load_material_texture(    obj::Model &model,
                                    obj::Material::Map &map,
                                    osg::StateSet *stateset,
                                    const unsigned int texture_unit,
                                    const osgDB::Options* options)
{
    std::string filename = map.name;
    if (!filename.empty())
    {
        osg::ref_ptr< osg::Image > image;
        if ( !model.getDatabasePath().empty() )
        {
            // first try with database path of parent.
            image = osgDB::readRefImageFile(model.getDatabasePath()+'/'+filename, options);
        }

        if ( !image.valid() )
        {
            // if not already set then try the filename as is.
            image = osgDB::readRefImageFile(filename, options);
        }

        if ( image.valid() )
        {
            osg::Texture2D* texture = new osg::Texture2D( image.get() );
            osg::Texture::WrapMode textureWrapMode;
            if(map.clamp == true)
            {
                textureWrapMode = osg::Texture::CLAMP_TO_BORDER;
                texture->setBorderColor(osg::Vec4(0.0,0.0,0.0,0.0));    // transparent
                //stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
                //stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
            }
            else
            {
                textureWrapMode = osg::Texture::REPEAT;
            }

            texture->setWrap(osg::Texture2D::WRAP_R, textureWrapMode);
            texture->setWrap(osg::Texture2D::WRAP_S, textureWrapMode);
            texture->setWrap(osg::Texture2D::WRAP_T, textureWrapMode);
            stateset->setTextureAttributeAndModes( texture_unit, texture,osg::StateAttribute::ON );

            if ( map.type == obj::Material::Map::REFLECTION )
            {
                osg::TexGen* texgen = new osg::TexGen;
                texgen->setMode(osg::TexGen::SPHERE_MAP);
                stateset->setTextureAttributeAndModes( texture_unit,texgen,osg::StateAttribute::ON );
            }

            if  ( image->isImageTranslucent())
            {
                OSG_INFO<<"Found transparent image"<<std::endl;
                stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
                stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
            }
        }
    }

    if (map.uScale != 1.0f || map.vScale != 1.0f ||
            map.uOffset != 0.0f || map.vOffset != 0.0f)
    {
        osg::Matrix mat;
        if (map.uScale != 1.0f || map.vScale != 1.0f)
        {
            OSG_DEBUG << "Obj TexMat scale=" << map.uScale << "," << map.vScale << std::endl;
            mat *= osg::Matrix::scale(map.uScale, map.vScale, 1.0);
        }
        if (map.uOffset != 0.0f || map.vOffset != 0.0f)
        {
            OSG_DEBUG << "Obj TexMat offset=" << map.uOffset << "," << map.uOffset << std::endl;
            mat *= osg::Matrix::translate(map.uOffset, map.vOffset, 0.0);
        }

        osg::TexMat* texmat = new osg::TexMat;
        texmat->setMatrix(mat);
        stateset->setTextureAttributeAndModes( texture_unit,texmat,osg::StateAttribute::ON );
    }
}