Example #1
0
TexturePtr TextureLoader::load( const std::string &name )
{
	TexturesMap::const_iterator it = m_loadedTextures.find( name );
	if( it!=m_loadedTextures.end() )
	{
		return it->second;
	}

	boost::filesystem::path path = m_searchPaths.find( name );
	if( path.empty() )
	{
		IECore::msg( IECore::Msg::Error, "IECoreGL::TextureLoader::load", boost::format( "Couldn't find \"%s\"." ) % name );
		m_loadedTextures[name] = 0; // to save us trying over and over again
		return 0;
	}

	IECore::ReaderPtr r = IECore::Reader::create( path.string() );
	if( !r )
	{
		IECore::msg( IECore::Msg::Error, "IECoreGL::TextureLoader::load", boost::format( "Couldn't create a Reader for \"%s\"." ) % path.string() );
		m_loadedTextures[name] = 0; // to save us trying over and over again
		return 0;
	}

	IECore::ObjectPtr o = r->read();
	IECore::ImagePrimitivePtr i = IECore::runTimeCast<IECore::ImagePrimitive>( o );
	if( !i )
	{
		IECore::msg( IECore::Msg::Error, "IECoreGL::TextureLoader::load", boost::format( "\"%s\" is not an image." ) % path.string() );
		m_loadedTextures[name] = 0; // to save us trying over and over again
		return 0;
	}

	TexturePtr t = 0;
	try
	{
		ToGLTextureConverterPtr converter = new ToGLTextureConverter( i );
		t = IECore::runTimeCast<Texture>( converter->convert() );
	}
	catch( const std::exception &e )
	{
		IECore::msg( IECore::Msg::Error, "IECoreGL::TextureLoader::load", boost::format( "Texture conversion failed for \"%s\" ( %s )." ) % path.string() % e.what() );
	}
	m_loadedTextures[name] = t;
	return t;
}