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

« back to all changes in this revision

Viewing changes to COLLADAMaya/src/COLLADAMayaLightExporter.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 COLLADAMaya.
 
5
 
 
6
    Portions of the code are:
 
7
    Copyright (c) 2005-2007 Feeling Software Inc.
 
8
    Copyright (c) 2005-2007 Sony Computer Entertainment America
 
9
    Copyright (c) 2004-2005 Alias Systems Corp.
 
10
        
 
11
    Licensed under the MIT Open Source License, 
 
12
    for details please see LICENSE file or the website
 
13
    http://www.opensource.org/licenses/mit-license.php
 
14
*/
 
15
 
 
16
#include "COLLADAMayaStableHeaders.h"
 
17
#include "COLLADAMayaSceneGraph.h"
 
18
#include "COLLADAMayaEffectExporter.h"
 
19
#include "COLLADAMayaLightExporter.h"
 
20
#include "COLLADAMayaExportOptions.h"
 
21
#include "COLLADAMayaAnimationExporter.h"
 
22
 
 
23
#include <maya/MFnLight.h>
 
24
#include <maya/MFnNonAmbientLight.h>
 
25
#include <maya/MFnAmbientLight.h>
 
26
#include <maya/MFnSpotLight.h>
 
27
 
 
28
 
 
29
namespace COLLADAMaya
 
30
{
 
31
 
 
32
    //---------------------------------------------------------------
 
33
    LightExporter::LightExporter ( 
 
34
        COLLADASW::StreamWriter* streamWriter,
 
35
        DocumentExporter* documentExporter )
 
36
    : COLLADASW::LibraryLights ( streamWriter )
 
37
    , mDocumentExporter ( documentExporter )
 
38
    {}
 
39
 
 
40
 
 
41
    //---------------------------------------------------------------
 
42
    void LightExporter::exportLights ( )
 
43
    {
 
44
        if ( !ExportOptions::exportLights() ) return;
 
45
 
 
46
        // Get the list with the transform nodes.
 
47
        SceneGraph* sceneGraph = mDocumentExporter->getSceneGraph();
 
48
        SceneElementsList* exportNodesTree = sceneGraph->getExportNodesTree();
 
49
 
 
50
        // Export all/selected DAG nodes
 
51
        size_t length = exportNodesTree->size();
 
52
        for ( uint i = 0; i < length; ++i )
 
53
        {
 
54
            SceneElement* sceneElement = ( *exportNodesTree ) [i];
 
55
            exportLights ( sceneElement );
 
56
        }
 
57
 
 
58
        closeLibrary();
 
59
    }
 
60
 
 
61
    //---------------------------------------------------------------
 
62
    void LightExporter::exportLights ( SceneElement* sceneElement )
 
63
    {
 
64
        // If we have a external reference, we don't need to export the data here.
 
65
        if ( !sceneElement->getIsLocal() ) return;
 
66
        if ( !sceneElement->getIsExportNode () ) return;
 
67
 
 
68
        // Check if it is a light.
 
69
        SceneElement::Type sceneElementType = sceneElement->getType();
 
70
        if ( sceneElementType == SceneElement::LIGHT )
 
71
        {
 
72
            // Get the current dag path
 
73
            MDagPath dagPath = sceneElement->getPath();
 
74
 
 
75
            // Check if the current scene element isn't already exported.
 
76
            SceneGraph* sceneGraph = mDocumentExporter->getSceneGraph();
 
77
            if ( sceneGraph->findExportedElement ( dagPath ) ) return;
 
78
 
 
79
            // Check if the current element is an instance. 
 
80
            // We don't need to export instances, because we export the original instanced element.
 
81
            bool isInstance = ( dagPath.isInstanced() && dagPath.instanceNumber() > 0 );
 
82
 
 
83
            // If the original instanced element isn't already exported, we have to export it now.
 
84
            if ( isInstance )
 
85
            {
 
86
                // Get the original instanced element.
 
87
                MDagPath instancedPath;
 
88
                dagPath.getPath ( instancedPath, 0 );
 
89
 
 
90
                // Check if the original instanced element is already exported.
 
91
                SceneGraph* sceneGraph = mDocumentExporter->getSceneGraph();
 
92
                SceneElement* exportedElement = sceneGraph->findExportedElement ( instancedPath );
 
93
                if ( exportedElement == 0 )
 
94
                {
 
95
                    // Export the original instanced element and push it in the exported scene graph. 
 
96
                    if ( exportLight ( instancedPath ) )
 
97
                    {
 
98
                        SceneElement* instancedSceneElement = sceneGraph->findElement ( instancedPath );
 
99
                        SceneGraph* sceneGraph = mDocumentExporter->getSceneGraph();
 
100
                        sceneGraph->addExportedElement( instancedSceneElement );
 
101
                    }
 
102
                }
 
103
            }
 
104
            else
 
105
            {
 
106
                // Export the element and push it in the exported scene graph. 
 
107
                if ( exportLight ( dagPath ) )
 
108
                {
 
109
                    SceneGraph* sceneGraph = mDocumentExporter->getSceneGraph();
 
110
                    sceneGraph->addExportedElement( sceneElement );
 
111
                }
 
112
            }
 
113
        }
 
114
 
 
115
 
 
116
        // Recursive call for all the child elements
 
117
        for ( uint i=0; i<sceneElement->getChildCount(); ++i )
 
118
        {
 
119
            SceneElement* childElement = sceneElement->getChild ( i );
 
120
            exportLights ( childElement );
 
121
        }
 
122
    }
 
123
 
 
124
    // ------------------------------------
 
125
    bool LightExporter::exportLight ( const MDagPath& dagPath )
 
126
    {
 
127
        if ( !ExportOptions::exportLights() ) return false;
 
128
 
 
129
        MObject lightNode = dagPath.node();
 
130
 
 
131
        // Retrieve the Maya light object
 
132
        MStatus status;
 
133
        MFnLight lightFn(lightNode, &status); CHECK_STAT(status);
 
134
        if (status != MStatus::kSuccess) return false;
 
135
 
 
136
        // Get the maya light id.
 
137
        String mayaLightId = mDocumentExporter->dagPathToColladaId ( dagPath );
 
138
 
 
139
        // Generate a COLLADA id for the new object
 
140
        String colladaLightId;
 
141
        
 
142
        // Check if there is an extra attribute "colladaId" and use this as export id.
 
143
        MString attributeValue;
 
144
        DagHelper::getPlugValue ( lightNode, COLLADA_ID_ATTRIBUTE_NAME, attributeValue );
 
145
        if ( attributeValue != EMPTY_CSTRING )
 
146
        {
 
147
            // Generate a valid collada name, if necessary.
 
148
            colladaLightId = mDocumentExporter->mayaNameToColladaName ( attributeValue, false );
 
149
        }
 
150
        else
 
151
        {
 
152
            // Generate a COLLADA id for the new object
 
153
            colladaLightId = mDocumentExporter->dagPathToColladaId ( dagPath );
 
154
        }
 
155
        // Make the id unique and store it in a map.
 
156
        colladaLightId = mLightIdList.addId ( colladaLightId );
 
157
        mMayaIdColladaIdMap [ mayaLightId ] = colladaLightId;
 
158
 
 
159
        // Get a pointer to the stream writer.
 
160
        COLLADASW::StreamWriter* streamWriter = mDocumentExporter->getStreamWriter();
 
161
 
 
162
        // The light name
 
163
        String lightName = mDocumentExporter->dagPathToColladaName ( dagPath );
 
164
 
 
165
        // Figure out the type of light and create it
 
166
        COLLADASW::Light* light = NULL;
 
167
        MFn::Type type = lightNode.apiType();
 
168
        switch (type)
 
169
        {
 
170
        case MFn::kAmbientLight: 
 
171
            light = new COLLADASW::AmbientLight( streamWriter, colladaLightId, lightName ); 
 
172
            break; 
 
173
        case MFn::kDirectionalLight: 
 
174
            light = new COLLADASW::DirectionalLight( streamWriter, colladaLightId, lightName ); 
 
175
            break;
 
176
        case MFn::kSpotLight: 
 
177
            light = new COLLADASW::SpotLight( streamWriter, colladaLightId, lightName ); 
 
178
            break;
 
179
        case MFn::kPointLight: // Intentional pass-through
 
180
        default: 
 
181
            light = new COLLADASW::PointLight( streamWriter, colladaLightId, lightName ); 
 
182
            break;
 
183
        }
 
184
 
 
185
        // Export the original maya name.
 
186
        light->addExtraTechniqueParameter ( PROFILE_MAYA, PARAMETER_MAYA_ID, mayaLightId );
 
187
 
 
188
        // Get a pointer to the animation exporter.
 
189
        AnimationExporter* anim = mDocumentExporter->getAnimationExporter();
 
190
        bool animated = false;
 
191
        
 
192
        // Color/Intensity are the common attributes of all lights
 
193
        MColor mayaColor = lightFn.color ( &status ); CHECK_STAT(status);
 
194
        COLLADASW::Color lightColor ( mayaColor.r, mayaColor.g, mayaColor.b, mayaColor.a );
 
195
        animated = anim->addNodeAnimation ( lightNode, ATTR_COLOR, kColour, RGBA_PARAMETERS );
 
196
        light->setColor( lightColor, animated );
 
197
 
 
198
        float intensity = lightFn.intensity ( &status ); CHECK_STAT(status);
 
199
        animated = anim->addNodeAnimation ( lightNode, ATTR_INTENSITY, kSingle );
 
200
        light->setIntensity( intensity, animated );
 
201
 
 
202
        // Add the type specific attributes
 
203
        if (lightNode.hasFn(MFn::kNonAmbientLight))
 
204
        {
 
205
            // Needed Point and Spot light types parameters: Attenuation and Attenuation_Scale
 
206
            // Attenuation in COLLADA is equal to Decay in Maya.
 
207
            MFnNonAmbientLight naLightFn(lightNode);
 
208
            int decayRate = naLightFn.decayRate(&status); CHECK_STAT(status);
 
209
            decayRate = std::min ( decayRate, 2 ); 
 
210
            decayRate = std::max ( decayRate, 0 );
 
211
 
 
212
            light->setConstantAttenuation ( ( decayRate == 0 ) ? 1.0f : 0.0f);
 
213
            light->setLinearAttenuation ( ( decayRate == 1 ) ? 1.0f : 0.0f);
 
214
            light->setQuadraticAttenuation ( ( decayRate == 2 ) ? 1.0f : 0.0f);
 
215
        }
 
216
        else if (lightNode.hasFn(MFn::kAmbientLight))
 
217
        {
 
218
            MFnAmbientLight ambientLightFn ( lightNode );
 
219
            float ambientShade = ambientLightFn.ambientShade();
 
220
            String paramSid = EMPTY_STRING;
 
221
            animated = anim->addNodeAnimation ( lightNode, ATTR_AMBIENT_SHADE, kSingle );
 
222
            if ( animated ) paramSid = ATTR_AMBIENT_SHADE;
 
223
            light->addExtraTechniqueParameter ( PROFILE_MAYA, MAYA_AMBIENTSHADE_LIGHT_PARAMETER, ambientShade, paramSid );
 
224
        }
 
225
 
 
226
        if (lightNode.hasFn(MFn::kSpotLight))
 
227
        {
 
228
            // Put in the needed spot light type attributes : Falloff, Falloff_Scale and Angle
 
229
            MFnSpotLight spotFn(lightNode);
 
230
 
 
231
            float fallOffAngle = COLLADABU::Math::Utils::radToDegF ( (float)spotFn.coneAngle( &status ) ); CHECK_STAT(status);
 
232
            animated = anim->addNodeAnimation ( lightNode, ATTR_CONE_ANGLE, ( SampleType ) ( kSingle | kAngle ) );
 
233
            light->setFallOffAngle ( fallOffAngle, animated );
 
234
 
 
235
            light->setFallOffExponent ( 1.0f );
 
236
 
 
237
            float penumbraValue = COLLADABU::Math::Utils::radToDegF ( (float)spotFn.penumbraAngle( &status ) ); CHECK_STAT(status);
 
238
            animated = anim->addNodeAnimation ( lightNode, ATTR_PENUMBRA_ANGLE, ( SampleType ) ( kSingle | kAngle ) );
 
239
            // TODO
 
240
//            FCDLightTools::LoadPenumbra(light, penumbraValue, colladaLight->GetOuterAngle().GetAnimated());
 
241
 
 
242
            // TODO
 
243
//             animated = anim->addNodeAnimation ( lightNode, ATTR_DROP_OFF, kSingle );
 
244
//             light->setDropOff ( (float) spotFn.dropOff ( &status ), animated ); CHECK_MSTATUS(status);
 
245
        }
 
246
        
 
247
        addLight ( *light );
 
248
        delete light;
 
249
 
 
250
        return true;
 
251
    }
 
252
 
 
253
    // ------------------------------------
 
254
    const String LightExporter::findColladaLightId ( const String& mayaLightId )
 
255
    {
 
256
        const StringToStringMap::const_iterator it = mMayaIdColladaIdMap.find ( mayaLightId );
 
257
        if ( it != mMayaIdColladaIdMap.end () )
 
258
        {
 
259
            return it->second;
 
260
        }
 
261
        return EMPTY_STRING;
 
262
    }
 
263
 
 
264
}
 
 
b'\\ No newline at end of file'