diff options
author | gman@google.com <gman@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-23 00:18:29 +0000 |
---|---|---|
committer | gman@google.com <gman@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-23 00:18:29 +0000 |
commit | cb27fe7df0f1c308ea5b87c5dc42a91a960d979e (patch) | |
tree | 0a9f5c760215d895243972a1030f76db19e40c4c /o3d/import | |
parent | 2383c9497d4ea626fd4612f979cf81cca5a4dbbe (diff) | |
download | chromium_src-cb27fe7df0f1c308ea5b87c5dc42a91a960d979e.zip chromium_src-cb27fe7df0f1c308ea5b87c5dc42a91a960d979e.tar.gz chromium_src-cb27fe7df0f1c308ea5b87c5dc42a91a960d979e.tar.bz2 |
Add --file_paths to converter to allow converter to run
with typical collada files found on the net.
Often someone will export a collada file with absolute paths
like "z:\someplace\somewhere\texture.tga" and
"k:\shared\stuff\othertexture.tga"
The converter needed a way to handle these without requiring
an artist to manually edit XML files.
I also refactored the original_data_ stuff to make sure
we don't get 2 datas for the same path. I'm doing that
in prepartion for shortening the paths where possible
using base-path or some mechanism.
Review URL: http://codereview.chromium.org/159118
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21352 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'o3d/import')
-rw-r--r-- | o3d/import/cross/collada.cc | 77 | ||||
-rw-r--r-- | o3d/import/cross/collada.h | 49 |
2 files changed, 89 insertions, 37 deletions
diff --git a/o3d/import/cross/collada.cc b/o3d/import/cross/collada.cc index fa06c70..0e88ae2 100644 --- a/o3d/import/cross/collada.cc +++ b/o3d/import/cross/collada.cc @@ -126,6 +126,44 @@ Matrix4 FMMatrix44ToMatrix4(const FMMatrix44& fmmatrix44) { } } // anonymous namespace +void ColladaDataMap::Clear() { + original_data_.clear(); +} + +bool ColladaDataMap::AddData(const FilePath& file_path, + const std::string& data, + ServiceLocator* service_locator) { + std::pair<OriginalDataMap::iterator, bool> result = + original_data_.insert(std::pair<FilePath, std::string>(file_path, data)); + if (!result.second) { + O3D_ERROR(service_locator) + << "Attempt to map 2 resources to the same file path:" + << FilePathToUTF8(file_path).c_str(); + } + return result.second; +} + +std::vector<FilePath> ColladaDataMap::GetOriginalDataFilenames() const { + std::vector<FilePath> result; + for (OriginalDataMap::const_iterator iter = original_data_.begin(); + iter != original_data_.end(); + ++iter) { + result.push_back(iter->first); + } + return result; +} + +const std::string& ColladaDataMap::GetOriginalData( + const FilePath& filename) const { + static const std::string empty; + OriginalDataMap::const_iterator entry = original_data_.find(filename); + if (entry != original_data_.end()) { + return entry->second; + } else { + return empty; + } +} + // Import the given COLLADA file or ZIP file into the given scene. // This is the external interface to o3d. bool Collada::Import(Pack* pack, @@ -178,7 +216,7 @@ Collada::~Collada() { void Collada::ClearData() { textures_.clear(); - original_data_.clear(); + original_data_map_.Clear(); effects_.clear(); shapes_.clear(); skinned_shapes_.clear(); @@ -194,26 +232,6 @@ void Collada::ClearData() { unique_filename_counter_ = 0; } -std::vector<FilePath> Collada::GetOriginalDataFilenames() const { - std::vector<FilePath> result; - for (OriginalDataMap::const_iterator iter = original_data_.begin(); - iter != original_data_.end(); - ++iter) { - result.push_back(iter->first); - } - return result; -} - -const std::string& Collada::GetOriginalData(const FilePath& filename) const { - static const std::string empty; - OriginalDataMap::const_iterator entry = original_data_.find(filename); - if (entry != original_data_.end()) { - return entry->second; - } else { - return empty; - } -} - // Import the given COLLADA file or ZIP file under the given parent node. // Parameters: // filename: The COLLADA or ZIPped COLLADA file to import. @@ -1660,6 +1678,11 @@ Texture* Collada::BuildTextureFromImage(FCDImage* image) { GetRelativePathIfPossible(base_path_, uri, &uri); } + if (!FindFile(options_.file_paths, file_path, &file_path)) { + O3D_ERROR(service_locator_) << "Could not find file: " << filename; + return NULL; + } + tex = Texture::Ref( pack_->CreateTextureFromFile(FilePathToUTF8(uri), file_path, @@ -1674,7 +1697,7 @@ Texture* Collada::BuildTextureFromImage(FCDImage* image) { // Cache the original data by URI so we can recover it later. std::string contents; file_util::ReadFileToString(file_path, &contents); - original_data_[uri] = contents; + original_data_map_.AddData(uri, contents, service_locator_); } if (tempfile.size() > 0) ZipArchive::DeleteFile(tempfile); @@ -1984,7 +2007,10 @@ Effect* Collada::BuildEffect(FCDocument* doc, FCDEffect* collada_effect) { } } else { FilePath temp_path = file_path; - GetRelativePathIfPossible(base_path_, temp_path, &temp_path); + if (!FindFile(options_.file_paths, temp_path, &temp_path)) { + O3D_ERROR(service_locator_) << "Could not find file: " << path; + return NULL; + } file_util::ReadFileToString(temp_path, &effect_string); } } @@ -2007,7 +2033,7 @@ Effect* Collada::BuildEffect(FCDocument* doc, FCDEffect* collada_effect) { } if (options_.keep_original_data) { // Cache the original data by URI so we can recover it later. - original_data_[file_path] = effect_string; + original_data_map_.AddData(file_path, effect_string, service_locator_); } } } else { @@ -2076,7 +2102,8 @@ Effect* Collada::BuildEffect(FCDocument* doc, FCDEffect* collada_effect) { } if (options_.keep_original_data) { // Cache the original data by URI so we can recover it later. - original_data_[file_path] = effect_string; + original_data_map_.AddData(file_path, effect_string, + service_locator_); } } } diff --git a/o3d/import/cross/collada.h b/o3d/import/cross/collada.h index a102e08..c2f659f 100644 --- a/o3d/import/cross/collada.h +++ b/o3d/import/cross/collada.h @@ -41,6 +41,7 @@ #include "base/file_path.h" #include "core/cross/param.h" #include "core/cross/types.h" +#include "utils/cross/file_path_utils.h" class FCDocument; class FCDAnimated; @@ -127,6 +128,31 @@ class NodeInstance { std::vector<NodeInstance *> children_; }; +class ColladaDataMap { + public: + // Adds a data to a filepath. Note, it is an error to associate more than one + // data to the same filepath. + bool AddData(const FilePath& file_path, + const std::string& data, + ServiceLocator* service_locator); + + // Access to the filenames of the original data for texture and + // sound assets imported when ImportFile was called. These will + // only return results after an import if the keep_original_data + // option was set to true when the Collada object was created. + std::vector<FilePath> GetOriginalDataFilenames() const; + const std::string& GetOriginalData(const FilePath& filename) const; + + // Clears the map. + void Clear(); + + private: + // A map containing the original data (still in original format) + // used to create the textures, sounds, etc., indexed by filename. + typedef std::map<FilePath, std::string> OriginalDataMap; + OriginalDataMap original_data_; +}; + class Collada { public: struct Options { @@ -153,6 +179,9 @@ class Collada { // The base path to use for determining the relative paths for // asset URIs. FilePath base_path; + + // A List of paths to search for files in. + std::vector<FilePath> file_paths; }; // Collada Param Names. @@ -215,16 +244,13 @@ class Collada { bool ImportFile(const FilePath& filename, Transform* parent, ParamFloat* animation_input); - // Access to the filenames of the original data for texture and - // sound assets imported when ImportFile was called. These will - // only return results after an import if the keep_original_data - // option was set to true when the Collada object was created. - std::vector<FilePath> GetOriginalDataFilenames() const; - const std::string& GetOriginalData(const FilePath& filename) const; - // Init the Collada Importer. static void Init(ServiceLocator* service_locator); + const ColladaDataMap& original_data_map() const { + return original_data_map_; + } + private: // Imports the given ZIP file into the given pack. bool ImportZIP(const FilePath& filename, Transform* parent, @@ -429,11 +455,6 @@ class Collada { // A map of the Textures created by the importer, indexed by filename. std::map<const std::wstring, Texture*> textures_; - // A map containing the original data (still in original format) - // used to create the textures, sounds, etc., indexed by filename. - typedef std::map<FilePath, std::string> OriginalDataMap; - OriginalDataMap original_data_; - // A map of the Effects created by the importer, indexed by DAE id. std::map<const std::string, Effect*> effects_; @@ -453,7 +474,11 @@ class Collada { // determining the relative paths to other files. FilePath base_path_; + // Original data by FilePath + ColladaDataMap original_data_map_; + ColladaZipArchive *collada_zip_archive_; + // Some temporaries used by the state importer bool cull_enabled_; bool cull_front_; |