Example #1
0
// create mtex, create texture, set texture image
MTex *DocumentImporter::create_texture(COLLADAFW::EffectCommon *ef, COLLADAFW::Texture &ctex, Material *ma,
                                       int i, TexIndexTextureArrayMap &texindex_texarray_map)
{
	COLLADAFW::SamplerPointerArray& samp_array = ef->getSamplerPointerArray();
	COLLADAFW::Sampler *sampler = samp_array[ctex.getSamplerId()];
		
	const COLLADAFW::UniqueId& ima_uid = sampler->getSourceImage();
	
	if (uid_image_map.find(ima_uid) == uid_image_map.end()) {
		fprintf(stderr, "Couldn't find an image by UID.\n");
		return NULL;
	}
	
	ma->mtex[i] = BKE_texture_mtex_add();
	ma->mtex[i]->texco = TEXCO_UV;
	ma->mtex[i]->tex = BKE_texture_add(G.main, "Texture");
	ma->mtex[i]->tex->type = TEX_IMAGE;
	ma->mtex[i]->tex->ima = uid_image_map[ima_uid];
	
	texindex_texarray_map[ctex.getTextureMapId()].push_back(ma->mtex[i]);
	
	return ma->mtex[i];
}
    //------------------------------
    bool LibraryEffectsLoader::fillSamplerArray ()
    {
        COLLADAFW::EffectCommon& commonEffect =  *mCurrentEffect->getCommonEffects().back();
        COLLADAFW::SamplerPointerArray& samplerArray = commonEffect.getSamplerPointerArray();

        // Iterate over the list of used samplers in the current effect profile 
        // and push them in the sampler array.
		size_t samplerCount = mEffectProfileSamplersMap.size();
		samplerArray.reallocMemory(samplerCount);
		samplerArray.setCount(samplerCount);
        StringIndexMap::const_iterator it = mEffectProfileSamplersMap.begin ();
        while ( it != mEffectProfileSamplersMap.end () )
        {
            String samplerSid = it->first;
			size_t samplerIndex = it->second;

            bool validSampler =  false;
            SidSamplerInfoMap::iterator samplerIt = mEffectProfileSidSamplerInfoMap.find ( samplerSid );
            
            if ( samplerIt == mEffectProfileSidSamplerInfoMap.end () )
            {
                samplerIt = mEffectSidSamplerInfoMap.find ( samplerSid );
                if ( samplerIt != mEffectSidSamplerInfoMap.end () ) validSampler = true;
            }
            else validSampler = true;
            
            if ( validSampler )
            {
                SamplerInfo& samplerInfo = samplerIt->second;
                samplerInfo.id = samplerArray.getCount();
                COLLADAFW::Sampler* sampler = samplerInfo.sampler;
                sampler->setSid(samplerSid);
                if ( !sampler->getSourceImage().isValid() )
                {
                    bool validSurface = false;
                    SidSurfaceMap::const_iterator surfaceIt = mEffectProfileSidSurfaceMap.find( samplerInfo.surfaceSid );
                    if ( surfaceIt == mEffectProfileSidSurfaceMap.end() )
                    {
                        surfaceIt = mEffectSidSurfaceMap.find( samplerInfo.surfaceSid );
                        if ( surfaceIt != mEffectSidSurfaceMap.end() ) validSurface = true;
                    }
                    else validSurface = true;
                    if ( validSurface )
                    {
                        const Surface& surface = surfaceIt->second;
                        sampler->setSource(surface.imageUniqueId);

                        // copy sampler into common effect
                        samplerArray[samplerIndex] = sampler->clone();
                    }
                }
                else
                {
					samplerArray[samplerIndex] = sampler->clone();
                }
            }
            else
			{
				// we a null sampler here, to ensure the index of all of the following sampler remain correct
				samplerArray[samplerIndex] = 0;

				if ( !handleFWLError ( SaxFWLError::ERROR_UNRESOLVED_REFERENCE, "No sampler for texture \"" + samplerSid + "\" defined!" ))
					return false;
			}
            ++it;
        }

		return true;
    }