2
Copyright (c) 2008-2009 NetAllied Systems GmbH
4
This file is part of DAE2MA.
6
Licensed under the MIT Open Source License,
7
for details please see LICENSE file or the website
8
http://www.opensource.org/licenses/mit-license.php
11
#include "DAE2MAStableHeaders.h"
12
#include "DAE2MAImageImporter.h"
13
#include "DAE2MAMaterialImporter.h"
15
#include <MayaDMCommands.h>
21
const String ImageImporter::IMAGE_NAME = "image";
24
//------------------------------
25
ImageImporter::ImageImporter ( DocumentImporter* documentImporter )
26
: BaseImporter ( documentImporter )
30
//------------------------------
31
ImageImporter::~ImageImporter()
33
// A copy of the framework's library image elements.
34
std::map<COLLADAFW::UniqueId, COLLADAFW::Image*>::iterator it = mImagesMap.begin ();
35
while ( it != mImagesMap.end () )
37
COLLADAFW::Image* image = it->second;
44
//------------------------------
45
void ImageImporter::storeImage ( const COLLADAFW::Image* image )
47
const COLLADAFW::UniqueId& imageId = image->getUniqueId ();
48
mImagesMap [imageId] = new COLLADAFW::Image ( *image );
51
//------------------------------
52
void ImageImporter::importImages ()
54
// TODO We have look for images, we have to import multiple times!
55
// It's possible, that we have to create an image for more than one time. This happens if:
56
// a) one image is referenced from multiple effects
57
// b) one image is referenced in multiple samplers in one effect
58
// c) one effect uses the same sampler multiple times.
59
// We have to dublicate the image, about the possibility to create multiple uv-sets on it.
61
EffectImporter* effectImporter = getDocumentImporter ()->getEffectImporter ();
62
EffectImporter::UniqueIdTextureInfosMap& effectTextureInfosMap = effectImporter->getTextureInfosMap ();
63
size_t numTextureInfos = effectTextureInfosMap.size ();
65
// We have to generate a separate maya file for every texture info, which use an image.
66
EffectImporter::UniqueIdTextureInfosMap::iterator it = effectTextureInfosMap.begin ();
67
while ( it != effectTextureInfosMap.end () )
69
const COLLADAFW::UniqueId& effectId = it->first;
70
std::vector<EffectImporter::TextureInfo>& textureInfos = it->second;
71
for ( size_t i=0; i<textureInfos.size (); ++i )
73
EffectImporter::TextureInfo& textureInfo = textureInfos [i];
74
const COLLADAFW::UniqueId& imageId = textureInfo.getImageId ();
76
const COLLADAFW::Image* image = findImage ( imageId );
79
std::cerr << "The image was not imported! " << std::endl;
83
// Import the images data.
84
importImage ( image, textureInfo );
91
//------------------------------
92
void ImageImporter::importImage (
93
const COLLADAFW::Image* image,
94
EffectImporter::TextureInfo& textureInfo )
96
// Check if the image is already imported.
97
const COLLADAFW::UniqueId& imageId = image->getUniqueId ();
99
// Create a unique name.
100
String imageName = image->getName ();
101
if ( imageName.empty () ) imageName = IMAGE_NAME;
102
imageName = DocumentImporter::frameworkNameToMayaName ( imageName );
103
const ExtraDataCallbackHandler& callbackHandler = getDocumentImporter ()->getMayaIdCallbackHandler ();
104
String originalMayaId = getOriginalMayaId ( callbackHandler, imageId, COLLADASaxFWL15::HASH_ELEMENT_IMAGE );
105
if ( !originalMayaId.empty () ) imageName = originalMayaId;
106
imageName = generateUniqueDependNodeName ( imageName );
108
// Get the maya ascii file.
109
FILE* file = getDocumentImporter ()->getFile ();
112
const COLLADABU::URI& imageURI = image->getImageURI ();
113
String fileTextureName = imageURI.toNativePath ();
114
COLLADABU::Utils::stringFindAndReplace ( fileTextureName, "\\", "/" );
115
if ( fileTextureName.at(0) == '/' )
116
fileTextureName = "." + fileTextureName;
118
//createNode file -name "file1";
119
// setAttr ".fileTextureName" -type "string" "./tex/checkerRGB.jpg";
120
MayaDM::File mayaImage ( file, imageName );
121
mayaImage.setFileTextureName ( fileTextureName );
123
// Add the original id attribute.
124
String colladaId = image->getOriginalId ();
125
if ( !colladaId.empty () )
127
MayaDM::addAttr ( file, COLLADA_ID_ATTRIBUTE_NAME, ATTRIBUTE_DATA_TYPE, ATTRIBUTE_TYPE_STRING );
128
MayaDM::setAttr ( file, COLLADA_ID_ATTRIBUTE_NAME, ATTRIBUTE_TYPE, ATTRIBUTE_TYPE_STRING, colladaId );
130
// // TODO Add the attributes for all the extra tags.
131
// setExtraData ( image->getExtraDataArray () );
133
// Push the maya image file in the map to the unique image id.
134
mImageIdMayaImageFileMap [ imageId ] = mayaImage;
136
// Set the generated maya image node into the current texture info element.
137
textureInfo.setImageNode ( mayaImage );
140
//------------------------------
141
const MayaDM::File* ImageImporter::findMayaImageFile ( const COLLADAFW::UniqueId& imageId )
143
UniqueIdMayaImagesMap::const_iterator it = mImageIdMayaImageFileMap.find ( imageId );
144
if ( it != mImageIdMayaImageFileMap.end () )
146
return &(it->second);
151
//------------------------------
152
const COLLADAFW::Image* ImageImporter::findImage ( const COLLADAFW::UniqueId& imageId )
154
std::map<COLLADAFW::UniqueId, COLLADAFW::Image*>::iterator it = mImagesMap.find ( imageId );
155
if ( it != mImagesMap.end () )
160
} // namespace DAE2MA