Initial support for FBX embedded textures

This commit is contained in:
Otger
2015-11-23 19:49:54 +01:00
parent 916cfb9d53
commit f94bc8d66e
7 changed files with 221 additions and 0 deletions

View File

@@ -54,6 +54,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "FBXUtil.h"
#include "FBXProperties.h"
#include "FBXImporter.h"
#include "StringComparison.h"
#include "../include/assimp/scene.h"
#include <boost/foreach.hpp>
#include <boost/scoped_array.hpp>
@@ -148,6 +149,7 @@ public:
std::for_each(animations.begin(),animations.end(),Util::delete_fun<aiAnimation>());
std::for_each(lights.begin(),lights.end(),Util::delete_fun<aiLight>());
std::for_each(cameras.begin(),cameras.end(),Util::delete_fun<aiCamera>());
std::for_each(textures.begin(),textures.end(),Util::delete_fun<aiTexture>());
}
@@ -1449,6 +1451,36 @@ private:
return static_cast<unsigned int>(materials.size() - 1);
}
// ------------------------------------------------------------------------------------------------
// Video -> aiTexture
unsigned int ConvertVideo(const Video& video)
{
// generate empty output texture
aiTexture* out_tex = new aiTexture();
textures.push_back(out_tex);
// assuming the texture is compressed
out_tex->mWidth = static_cast<unsigned int>(video.ContentLength()); // total data size
out_tex->mHeight = 0; // fixed to 0
// steal the data from the Video to avoid an additional copy
out_tex->pcData = reinterpret_cast<aiTexel*>( const_cast<Video&>(video).RelinquishContent() );
// try to extract a hint from the file extension
const std::string& filename = video.FileName().empty() ? video.RelativeFilename() : video.FileName();
std::string ext = BaseImporter::GetExtension(filename);
if(ext == "jpeg") {
ext = "jpg";
}
if(ext.size() <= 3) {
memcpy(out_tex->achFormatHint, ext.c_str(), ext.size());
}
return static_cast<unsigned int>(textures.size() - 1);
}
// ------------------------------------------------------------------------------------------------
void TrySetTextureProperties(aiMaterial* out_mat, const TextureMap& textures,
@@ -1466,6 +1498,24 @@ private:
aiString path;
path.Set(tex->RelativeFilename());
const Video* media = tex->Media();
if(media != 0) {
unsigned int index;
VideoMap::const_iterator it = textures_converted.find(media);
if(it != textures_converted.end()) {
index = (*it).second;
}
else {
index = ConvertVideo(*media);
textures_converted[media] = index;
}
// setup texture reference string (copied from ColladaLoader::FindFilenameForEffectTexture)
path.data[0] = '*';
path.length = 1 + ASSIMP_itoa10(path.data + 1, MAXLEN - 1, index);
}
out_mat->AddProperty(&path,_AI_MATKEY_TEXTURE_BASE,target,0);
aiUVTransform uvTrafo;
@@ -3024,6 +3074,13 @@ private:
std::swap_ranges(cameras.begin(),cameras.end(),out->mCameras);
}
if(textures.size()) {
out->mTextures = new aiTexture*[textures.size()]();
out->mNumTextures = static_cast<unsigned int>(textures.size());
std::swap_ranges(textures.begin(),textures.end(),out->mTextures);
}
}
@@ -3037,10 +3094,14 @@ private:
std::vector<aiAnimation*> animations;
std::vector<aiLight*> lights;
std::vector<aiCamera*> cameras;
std::vector<aiTexture*> textures;
typedef std::map<const Material*, unsigned int> MaterialMap;
MaterialMap materials_converted;
typedef std::map<const Video*, unsigned int> VideoMap;
VideoMap textures_converted;
typedef std::map<const Geometry*, std::vector<unsigned int> > MeshMap;
MeshMap meshes_converted;