File: daeWriter.h

package info (click to toggle)
openscenegraph-3.4 3.4.1+dfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 36,912 kB
  • sloc: cpp: 392,492; ansic: 23,625; java: 1,020; yacc: 548; makefile: 449; objc: 288; xml: 155; lex: 151
file content (355 lines) | stat: -rw-r--r-- 11,940 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 * Copyright 2006 Sony Computer Entertainment Inc.
 *
 * Licensed under the SCEA Shared Source License, Version 1.0 (the "License"); you may not use this
 * file except in compliance with the License. You may obtain a copy of the License at:
 * http://research.scea.com/scea_shared_source_license.html
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing permissions and limitations under the
 * License.
 */

#ifndef _DAE_WRITER_H_
#define _DAE_WRITER_H_

#include <map>
#include <stack>

#include <osg/Node>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Group>
#include <osg/LightSource>
#include <osg/Camera>
#include <osg/Material>
#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>
#include <osg/Switch>
#include <osg/StateSet>
#include <osg/LOD>
#include <osg/ProxyNode>
#include <osg/CoordinateSystemNode>
#include <osg/BlendColor>
#include <osg/BlendFunc>

#include <osg/Notify>
#include <osg/NodeVisitor>
#include <osgDB/FileNameUtils>
#include <osgDB/FileUtils>
#include <osgDB/Registry>
#include <osgDB/ExternalFileWriter>
#include <osgSim/MultiSwitch>
#include <osgAnimation/AnimationManagerBase>
#include <osgAnimation/UpdateBone>
#include <osgAnimation/RigGeometry>
#include <osgAnimation/MorphGeometry>

#include <dae.h>
#include <dae/daeDocument.h>
#include <dom/domChannel.h>

#ifdef COLLADA_DOM_2_4_OR_LATER
namespace ColladaDOM141
{
#endif

class domCOLLADA;
class domGeometry;
class domInstance_geometry;
class domLibrary_cameras;
class domLibrary_effects;
class domLibrary_geometries;
class domLibrary_lights;
class domLibrary_materials;
class domLibrary_visual_scenes;
class domLibrary_animations;
class domMaterial;
class domMesh;
class domNode;
class domSource;
class domVisual_scene;
class domP;

#ifdef COLLADA_DOM_2_4_OR_LATER
}

using namespace ColladaDOM141;
#endif

namespace osgDAE {

/// Convert value to string using it's stream operator
template <typename T>
std::string toString(T value)
{
    std::stringstream str;
    str << value;
    return str.str();
}

std::string toString(const osg::Vec2f& value);
std::string toString(const osg::Vec2d& value);
std::string toString(const osg::Vec3f& value);
std::string toString(const osg::Vec3d& value);
std::string toString(const osg::Vec4f& value);
std::string toString(const osg::Vec4d& value);
std::string toString(const osg::Matrixf& value);
std::string toString(const osg::Matrixd& value);

// Collects all nodes that are targeted by an animation
class FindAnimatedNodeVisitor : public osg::NodeVisitor
{
public:
    FindAnimatedNodeVisitor():
        osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
        {}

    virtual void apply(osg::Node& node)
    {
        osg::Callback* ncb = node.getUpdateCallback();
        if (ncb)
        {
            osgAnimation::AnimationUpdateCallback<osg::NodeCallback>* ut = dynamic_cast<osgAnimation::AnimationUpdateCallback<osg::NodeCallback>*>(ncb);
            if (ut)
            {
                if (_updateCallbackNameNodeMap[ut->getName()] == NULL)
                {
                    _updateCallbackNameNodeMap[ut->getName()] = &node;
                }
                else
                {
                    // TODO store in a multimap and let the exporter create multiple <channel>s for each connected node
                    OSG_WARN << "Multiple nodes using the same update callback not supported" << std::endl;
                }
            }
        }
        traverse(node);
    }

    osg::Node* getTargetNode(const std::string& targetName)
    {
        UpdateCallbackNameNodeMap::iterator it = _updateCallbackNameNodeMap.find(targetName);
        if (it != _updateCallbackNameNodeMap.end())
        {
            return it->second;
        }
        return NULL;
    }

private:
    typedef std::map< std::string, osg::Node*>    UpdateCallbackNameNodeMap;
    UpdateCallbackNameNodeMap _updateCallbackNameNodeMap;
};

/**
@class daeWriter
@brief Write a OSG scene into a DAE file
*/
class daeWriter : public osg::NodeVisitor
{
protected:
    class ArrayNIndices;
public:
    struct Options
    {
        Options();

        bool usePolygons;
        /** work in Google compatibility mode. In daeWMaterials, change transparency color. And in daeWGeometry, replace tristrip and trifans by triangles*/
        bool googleMode;
        /** Write OSG specific data as extra data. */
        bool writeExtras;
        /** work in Google compatibility mode for textures*/
        bool earthTex;
        /** link to original images instead of exporting */
        bool linkOrignialTextures;
        /** force the use an image for a texture, even if the file is not found (when m_linkOrignialTextures). */
        bool forceTexture;
        bool namesUseCodepage;
        unsigned int relativiseImagesPathNbUpDirs;
        /** Rename all IDs (geometries, materials, etc.) to remove characters which may be interpreted as an URI. */
        bool renameIds;
    };
    daeWriter(DAE *dae_, const std::string &fileURI, const std::string & directory, const std::string & srcDirectory, const osgDB::ReaderWriter::Options * options, TraversalMode tm=TRAVERSE_ALL_CHILDREN, const Options * pluginOptions=NULL);
    virtual ~daeWriter();

    void setRootNode( const osg::Node &node );

    bool isSuccess() { return success; }

    bool writeFile();

    virtual void    apply( osg::Node &node );
    virtual void    apply( osg::Geode &node );
    virtual void    apply( osg::Group &node );
    virtual void    apply( osg::LightSource &node );
    virtual void    apply( osg::Camera &node );
    virtual void    apply( osg::MatrixTransform &node );
    virtual void    apply( osg::PositionAttitudeTransform &node );
    virtual void    apply( osg::Switch &node );
    virtual void    apply( osg::Sequence &node );
    virtual void    apply( osg::LOD &node );

    //virtual void    apply( osg::Billboard &node);
    virtual void    apply( osg::ProxyNode &node );
    //virtual void  apply( osg::Projection &node)
    virtual void    apply( osg::CoordinateSystemNode &node );
    //virtual void  apply( osg::ClipNode &node)
    //virtual void  apply( osg::TexGenNode &node)
    virtual void    apply( osg::Transform &node );
    virtual void    apply( osg::CameraView &node);
    //virtual void  apply( osg::PagedLOD &node)
    //virtual void  apply( osg::ClearNode &node)
    //virtual void  apply( osg::OccluderNode &node)

    void traverse (osg::Node &node);




protected: //methods

    void writeAnimations(osg::Node& node);
    void writeNodeExtra(osg::Node &node);
    void writeUpdateTransformElements(const osg::Vec3 &pos, const osg::Quat &q,    const osg::Vec3 &s);
    void writeRigGeometry(osgAnimation::RigGeometry *pOsgRigGeometry);
    void writeMorphGeometry(osgAnimation::MorphGeometry *pOsgMorphGeometry);

    void debugPrint( osg::Node &node );

    domGeometry* getOrCreateDomGeometry(osg::Geometry* pOsgGeometry);
    bool processGeometry( osg::Geometry *geom, domGeometry *geo, const std::string &name );
    domSource* createSource( daeElement *parent, const std::string &baseName, int size, bool color = false, bool uv = false );
    template < typename Ty >
        Ty *createPrimGroup( daeString type, domMesh *mesh, domSource *norm, domSource *color, const std::vector< domSource* > &texcoord );

    void processMaterial( osg::StateSet *ss, domBind_material *pDomBindMaterial, const std::string &geoName );

    void createAssetTag(bool isZUpAxis);

    // Overloaded version of createAssetTag which provides ability to
    // set user defined values for child elements
    void createAssetTag(const osg::Node &node);

    void pushStateSet(osg::StateSet* ss);

    void popStateSet(osg::StateSet* ss);

protected: //members
    DAE *dae;
    daeDocument *doc;
    domCOLLADA *dom;
    domLibrary_cameras *lib_cameras;
    domLibrary_effects *lib_effects;
    domLibrary_controllers *lib_controllers;
    domLibrary_geometries *lib_geoms;
    domLibrary_lights *lib_lights;
    domLibrary_materials *lib_mats;
    domLibrary_visual_scenes *lib_vis_scenes;
    domLibrary_animations* _domLibraryAnimations;
    domNode *currentNode;
    domVisual_scene *vs;

    bool success;
    unsigned int lastDepth;

  struct CompareStateSet
  {
      bool operator()(const osg::ref_ptr<osg::StateSet>& ss1, const osg::ref_ptr<osg::StateSet>& ss2) const
    {
      //std::cout << "CompareStateSet: " << ss1->compare(*ss2, false) << " " << ss1 << " " << ss2 << std::endl;
      return ss1->compare(*ss2, true) < 0;
    }
  };


    typedef std::map< osg::ref_ptr<osg::StateSet>, domMaterial *, CompareStateSet> MaterialMap;
    typedef std::stack<osg::ref_ptr<osg::StateSet> > StateSetStack;
    typedef std::map< osg::Geometry*, domGeometry *> OsgGeometryDomGeometryMap;
    typedef std::map< osgAnimation::RigGeometry*, domController *> OsgRigGeometryDomControllerMap;
    typedef std::map< osgAnimation::MorphGeometry*, domController *> OsgMorphGeometryDomControllerMap;

    typedef std::map< std::string, int > UniqueNames;
    UniqueNames uniqueNames;
    OsgGeometryDomGeometryMap geometryMap;
    OsgRigGeometryDomControllerMap _osgRigGeometryDomControllerMap;
    OsgMorphGeometryDomControllerMap _osgMorphGeometryDomControllerMap;

    MaterialMap materialMap;
    StateSetStack stateSetStack;

    osg::ref_ptr<osg::StateSet> currentStateSet;

    daeURI rootName;

    osg::StateSet* CleanStateSet(osg::StateSet* pStateSet) const;

    void updateCurrentDaeNode();

protected: //inner classes
    class ArrayNIndices
    {
    public:
        enum Mode { NONE, VEC2F, VEC2D, VEC3F, VEC3D, VEC4F, VEC4D, VEC4_UB };

        osg::Vec2Array*         vec2;
        osg::Vec3Array*         vec3;
        osg::Vec4Array*         vec4;
        osg::Vec2dArray*        vec2d;
        osg::Vec3dArray*        vec3d;
        osg::Vec4dArray*        vec4d;
        osg::Vec4ubArray*       vec4ub;

        osg::Array*             valArray;
        osg::IndexArray*        inds;

        ArrayNIndices( osg::Array* valArray, osg::IndexArray* ind );

        Mode getMode() const { return mode; }

        unsigned int getDAESize();

        /// Appends the contained OSG vector array to a domListOfFloats
        bool append(domListOfFloats & list);
    protected:
        Mode mode;
    };

private: //members

        /** append elements (verts, normals, colors and texcoord) for file write */
        void appendGeometryIndices(osg::Geometry *geom,
                                          domP * p,
                                          unsigned int vindex,
                                          domSource * norm,
                                          domSource * color,
                                          const ArrayNIndices & verts,
                                          const ArrayNIndices & normals,
                                          const ArrayNIndices & colors,
                                          const std::vector<ArrayNIndices> & texcoords,
                                          unsigned int  ncount,
                                          unsigned int  ccount);

        /** provide a name to node */
        std::string getNodeName(const osg::Node & node,const std::string & defaultName);

        /** provide an unique name */
        std::string uniquify( const std::string &name );

        /** Current RenderingHint */
        /** This are needed because the stateSet merge code currently does not handle it */
        int m_CurrentRenderingHint;

        FindAnimatedNodeVisitor _animatedNodeCollector;

        const osgDB::ReaderWriter::Options * _options;
        Options _pluginOptions;
        osgDB::ExternalFileWriter _externalWriter;
};

}

#endif