~ubuntu-branches/ubuntu/wily/opencollada/wily-proposed

« back to all changes in this revision

Viewing changes to dae2ma/src/DAE2MAImageImporter.cpp

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2015-05-14 17:23:27 UTC
  • Revision ID: package-import@ubuntu.com-20150514172327-f862u8envms01fra
Tags: upstream-0.1.0~20140703.ddf8f47+dfsg1
ImportĀ upstreamĀ versionĀ 0.1.0~20140703.ddf8f47+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 2008-2009 NetAllied Systems GmbH
 
3
 
 
4
    This file is part of DAE2MA.
 
5
 
 
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
 
9
*/
 
10
 
 
11
#include "DAE2MAStableHeaders.h"
 
12
#include "DAE2MAImageImporter.h"
 
13
#include "DAE2MAMaterialImporter.h"
 
14
 
 
15
#include <MayaDMCommands.h>
 
16
 
 
17
 
 
18
namespace DAE2MA
 
19
{
 
20
 
 
21
    const String ImageImporter::IMAGE_NAME = "image";
 
22
 
 
23
 
 
24
    //------------------------------
 
25
        ImageImporter::ImageImporter ( DocumentImporter* documentImporter )
 
26
        : BaseImporter ( documentImporter )
 
27
        {
 
28
        }
 
29
        
 
30
    //------------------------------
 
31
        ImageImporter::~ImageImporter()
 
32
        {
 
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 () )
 
36
        {
 
37
            COLLADAFW::Image* image = it->second;
 
38
            delete image;
 
39
            ++it;
 
40
        }
 
41
        mImagesMap.clear ();
 
42
        }
 
43
 
 
44
    //------------------------------
 
45
    void ImageImporter::storeImage ( const COLLADAFW::Image* image )
 
46
    {
 
47
        const COLLADAFW::UniqueId& imageId = image->getUniqueId ();
 
48
        mImagesMap [imageId] = new COLLADAFW::Image ( *image );
 
49
    }
 
50
 
 
51
    //------------------------------
 
52
    void ImageImporter::importImages ()
 
53
    {
 
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.
 
60
 
 
61
        EffectImporter* effectImporter = getDocumentImporter ()->getEffectImporter ();
 
62
        EffectImporter::UniqueIdTextureInfosMap& effectTextureInfosMap = effectImporter->getTextureInfosMap ();
 
63
        size_t numTextureInfos = effectTextureInfosMap.size ();
 
64
 
 
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 () )
 
68
        {
 
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 )
 
72
            {
 
73
                EffectImporter::TextureInfo& textureInfo = textureInfos [i];
 
74
                const COLLADAFW::UniqueId& imageId = textureInfo.getImageId ();
 
75
 
 
76
                const COLLADAFW::Image* image = findImage ( imageId );
 
77
                if ( image == 0 ) 
 
78
                {
 
79
                    std::cerr << "The image was not imported! " << std::endl;
 
80
                    continue;
 
81
                }
 
82
 
 
83
                // Import the images data.
 
84
                importImage ( image, textureInfo );
 
85
            }
 
86
 
 
87
            ++it;
 
88
        }
 
89
    }
 
90
 
 
91
    //------------------------------
 
92
    void ImageImporter::importImage ( 
 
93
        const COLLADAFW::Image* image, 
 
94
        EffectImporter::TextureInfo& textureInfo )
 
95
    {
 
96
        // Check if the image is already imported.
 
97
        const COLLADAFW::UniqueId& imageId = image->getUniqueId ();
 
98
 
 
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 );
 
107
 
 
108
        // Get the maya ascii file.
 
109
        FILE* file = getDocumentImporter ()->getFile ();
 
110
 
 
111
        // Get the image uri
 
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;
 
117
        
 
118
        //createNode file -name "file1";
 
119
        //    setAttr ".fileTextureName" -type "string" "./tex/checkerRGB.jpg";
 
120
        MayaDM::File mayaImage ( file, imageName );
 
121
        mayaImage.setFileTextureName ( fileTextureName );
 
122
 
 
123
        // Add the original id attribute.
 
124
        String colladaId = image->getOriginalId ();
 
125
        if ( !colladaId.empty () )
 
126
        {
 
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 );
 
129
        }
 
130
//         // TODO Add the attributes for all the extra tags.
 
131
//         setExtraData ( image->getExtraDataArray () );
 
132
 
 
133
        // Push the maya image file in the map to the unique image id.
 
134
        mImageIdMayaImageFileMap [ imageId ] = mayaImage;
 
135
 
 
136
        // Set the generated maya image node into the current texture info element.
 
137
        textureInfo.setImageNode ( mayaImage );
 
138
    }
 
139
 
 
140
    //------------------------------
 
141
    const MayaDM::File* ImageImporter::findMayaImageFile ( const COLLADAFW::UniqueId& imageId )
 
142
    {
 
143
        UniqueIdMayaImagesMap::const_iterator it = mImageIdMayaImageFileMap.find ( imageId );
 
144
        if ( it != mImageIdMayaImageFileMap.end () )
 
145
        {
 
146
            return &(it->second);
 
147
        }
 
148
        return 0;
 
149
    }
 
150
 
 
151
    //------------------------------
 
152
    const COLLADAFW::Image* ImageImporter::findImage ( const COLLADAFW::UniqueId& imageId )
 
153
    {
 
154
        std::map<COLLADAFW::UniqueId, COLLADAFW::Image*>::iterator it = mImagesMap.find ( imageId );
 
155
        if ( it != mImagesMap.end () ) 
 
156
            return it->second;
 
157
        return 0;
 
158
    }
 
159
 
 
160
} // namespace DAE2MA