root/OpenSceneGraph/trunk/src/osgPlugins/dae/daeReader.h @ 13041

Revision 13041, 16.7 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2 * Copyright 2006 Sony Computer Entertainment Inc.
3 *
4 * Licensed under the SCEA Shared Source License, Version 1.0 (the "License"); you may not use this
5 * file except in compliance with the License. You may obtain a copy of the License at:
6 * http://research.scea.com/scea_shared_source_license.html
7 *
8 * Unless required by applicable law or agreed to in writing, software distributed under the License
9 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
10 * implied. See the License for the specific language governing permissions and limitations under the
11 * License.
12 */
13
14#ifndef _DAE_CONV_H_
15#define _DAE_CONV_H_
16
17#include <string>
18
19#include <dae.h>
20#include <dae/daeURI.h>
21#include <dae/daeElement.h>
22#include <dom/domCommon_color_or_texture_type.h>
23#include <dom/domInputLocalOffset.h>
24#include <dom/domInstance_controller.h>
25
26#include <osg/Node>
27#include <osg/Notify>
28#include <osgDB/ReaderWriter>
29#include <osgDB/FileNameUtils>
30#include <osgDB/FileUtils>
31#include <osgDB/Registry>
32#include <osg/Material>
33#include <osg/Texture2D>
34#include <osgAnimation/BasicAnimationManager>
35#include <osgAnimation/Bone>
36#include <osgAnimation/Skeleton>
37
38class domBind_material;
39class domCamera;
40//class domCommon_color_or_texture_type;
41class domCommon_float_or_param_type;
42class domGeometry;
43class domInstance_controller;
44class domInstance_geometry;
45class domInstanceWithExtra;
46class domLight;
47class domLookat;
48class domMatrix;
49class domNode;
50class domP;
51class domProfile_COMMON;
52class domScale;
53class domSkew;
54class domTranslate;
55class domRotate;
56class domVisual_scene;
57
58namespace osgDAE
59{
60
61class domSourceReader;
62
63inline daeElement *getElementFromURI( daeURI &uri )
64{
65    if ( uri.getState() == daeURI::uri_loaded || uri.getState() == daeURI::uri_pending ) {
66        uri.resolveElement();
67    }
68    return uri.getElement();
69}
70inline daeElement *getElementFromIDRef( daeIDRef &idref )
71{
72    if ( idref.getState() == daeIDRef::id_loaded || idref.getState() == daeIDRef::id_pending ) {
73        idref.resolveElement();
74    }
75    return idref.getElement();
76}
77
78template< typename TInputArray, typename TInputType >
79bool findInputSourceBySemantic( TInputArray& inputs, const char* semantic, daeElement *& element,
80                                TInputType ** input = NULL, int unit = 0 )
81{
82    element = NULL;
83    int count = 0;
84    for ( size_t i = 0; i < inputs.getCount(); i++ ) {
85        if ( !strcmp(semantic, inputs[i]->getSemantic()) ) {
86            if ( count == unit )
87            {
88                element = getElementFromURI( inputs[i]->getSource() );
89                *input = (TInputType*)inputs[i];
90                return true;
91            }
92            count++;
93        }
94    }
95    return false;
96}
97
98/// Convert string to value using it's stream operator
99template <typename T>
100T parseString(const std::string& valueAsString) {
101    std::stringstream str;
102    str << valueAsString;
103    T result;
104    str >> result;
105    return result;
106}
107
108inline osg::Vec3 parseVec3String(const std::string& valueAsString)
109{
110    std::stringstream str;
111    str << valueAsString;
112    osg::Vec3 result;
113    str >> result.x() >> result.y() >> result.z();
114    return result;
115}
116
117inline osg::Matrix parseMatrixString(const std::string& valueAsString)
118{
119    std::stringstream str;
120    str << valueAsString;
121    osg::Matrix result;
122    str >> result(0,0) >> result(1,0) >> result(2,0) >> result(3,0)
123        >> result(0,1) >> result(1,1) >> result(2,1) >> result(3,1)
124        >> result(0,2) >> result(1,2) >> result(2,2) >> result(3,2)
125        >> result(0,3) >> result(1,3) >> result(2,3) >> result(3,3);
126    return result;
127}
128
129
130/**
131@class daeReader
132@brief Read a OSG scene from a DAE file
133*/
134class daeReader {
135public:
136    enum TessellateMode
137    {
138        TESSELLATE_NONE,                 ///< Do not tessellate at all (Polygons are stored as GL_POLYGON - not suitable for concave polygons)
139        TESSELLATE_POLYGONS_AS_TRIFAN,   ///< Tessellate the old way, interpreting polygons as triangle fans (faster, but does not work for concave polygons)
140        TESSELLATE_POLYGONS              ///< Use full tessellation of polygons (slower, works for concave polygons)
141    };
142
143    struct Options
144    {
145        Options();
146        bool strictTransparency;
147        int precisionHint;              ///< Precision hint flags, as specified in osgDB::Options::PrecisionHint
148        bool usePredefinedTextureUnits;
149        TessellateMode tessellateMode;
150    };
151
152    daeReader(DAE *dae_, const Options * pluginOptions);
153    virtual ~daeReader();
154
155    bool convert( const std::string &fileURI );
156
157    osg::Node* getRootNode()    { return _rootNode; }
158
159    const std::string& getAssetUnitName() const {return _assetUnitName;}
160    float getAssetUnitMeter() const {return _assetUnitMeter;}
161    domUpAxisType getAssetUpAxis() const {return _assetUp_axis;}
162
163    enum TextureUnitUsage
164    {
165        AMBIENT_OCCLUSION_UNIT = 0,
166        MAIN_TEXTURE_UNIT,
167        TRANSPARENCY_MAP_UNIT
168    };
169
170    enum InterpolationType
171    {
172        INTERPOLATION_UNKNOWN,
173        INTERPOLATION_STEP,
174        INTERPOLATION_LINEAR,
175        INTERPOLATION_BEZIER,
176        INTERPOLATION_HERMITE,
177        INTERPOLATION_CARDINAL,
178        INTERPOLATION_BSPLINE,
179
180        //COLLADA spec states that if interpolation is not specified then
181        //interpolation is application defined. Linear is a sensible default.
182        INTERPOLATION_DEFAULT = INTERPOLATION_LINEAR
183    };
184
185    enum AuthoringTool
186    {
187        UNKNOWN,
188        BLENDER,
189        DAZ_STUDIO,
190        FBX_CONVERTER,
191        AUTODESK_3DS_MAX = FBX_CONVERTER,//3ds Max exports to DAE via Autodesk's FBX converter
192        GOOGLE_SKETCHUP,
193        MAYA
194    };
195
196    class TextureParameters
197    {
198    public:
199        TextureParameters()
200            : wrap_s(osg::Texture::REPEAT), wrap_t(osg::Texture::REPEAT),
201            filter_min(osg::Texture::LINEAR_MIPMAP_LINEAR), filter_mag(osg::Texture::LINEAR),
202            transparent(false), opaque(FX_OPAQUE_ENUM_A_ONE), transparency(1.0f)
203        {}
204
205        bool operator < (const TextureParameters& rhs) const
206        {
207            int diffStr = filename.compare(rhs.filename);
208            if (diffStr) return diffStr < 0;
209            if (wrap_s != rhs.wrap_s) return wrap_s < rhs.wrap_s;
210            if (wrap_t != rhs.wrap_t) return wrap_t < rhs.wrap_t;
211            if (filter_min != rhs.filter_min) return filter_min < rhs.filter_min;
212            if (filter_mag != rhs.filter_mag) return filter_mag < rhs.filter_mag;
213            if (transparency != rhs.transparency) return transparency < rhs.transparency;
214            if (opaque != rhs.opaque) return opaque < rhs.opaque;
215            if (transparent != rhs.transparent) return transparent < rhs.transparent;
216            return border < rhs.border;
217        }
218
219        std::string filename;
220        osg::Texture::WrapMode wrap_s, wrap_t;
221        osg::Texture::FilterMode filter_min, filter_mag;
222        osg::Vec4 border;
223
224        //The following parameters are for transparency textures, to handle
225        //COLLADA's horrible transparency spec.
226        bool transparent;
227        domFx_opaque_enum opaque;
228        float transparency;
229    };
230
231    class ChannelPart : public osg::Referenced
232    {
233    public:
234        std::string name;
235        osg::ref_ptr<osgAnimation::KeyframeContainer> keyframes;
236        InterpolationType interpolation;
237    };
238
239    typedef std::map<domGeometry*, osg::ref_ptr<osg::Geode> >    domGeometryGeodeMap;
240    typedef std::map<domMaterial*, osg::ref_ptr<osg::StateSet> > domMaterialStateSetMap;
241    typedef std::map<std::string, osg::ref_ptr<osg::StateSet> >    MaterialStateSetMap;
242    typedef std::multimap< daeElement*, domChannel*> daeElementDomChannelMap;
243    typedef std::map<domChannel*, osg::ref_ptr<osg::NodeCallback> > domChannelOsgAnimationUpdateCallbackMap;
244    typedef std::map<domNode*, osg::ref_ptr<osgAnimation::Bone> > domNodeOsgBoneMap;
245    typedef std::map<domNode*, osg::ref_ptr<osgAnimation::Skeleton> > domNodeOsgSkeletonMap;
246    typedef std::map<TextureParameters, osg::ref_ptr<osg::Texture2D> > TextureParametersMap;
247    typedef std::map<std::pair<const osg::StateSet*, TextureUnitUsage>, std::string> TextureToCoordSetMap;
248
249    typedef std::map< daeElement*, domSourceReader > SourceMap;
250    typedef std::map< int, osg::IntArray*, std::less<int> > IndexMap;
251    typedef std::map< int, osg::Array*, std::less<int> > ArrayMap;
252
253    typedef std::multimap< osgAnimation::Target*, osg::ref_ptr<ChannelPart> > TargetChannelPartMap;
254    typedef std::multimap<std::pair<const domMesh*, unsigned>, std::pair<osg::ref_ptr<osg::Geometry>, GLuint> > OldToNewIndexMap;
255
256private:
257    // If the node is a bone then it should be added before any other types of
258    // node, this function makes that happen.
259    static void addChild(osg::Group*, osg::Node*);
260
261     //scene processing
262    osg::Group* turnZUp();
263    osg::Group*    processVisualScene( domVisual_scene *scene );
264    osg::Node*    processNode( domNode *node, bool skeleton );
265    osg::Transform*    processOsgMatrixTransform( domNode *node, bool isBone);
266
267    template <typename T>
268    inline void getTransparencyCounts(daeDatabase*, int& zero, int& one) const;
269
270    /** Earlier versions of the COLLADA 1.4 spec state that transparency values
271    of 0 mean 100% opacity, but this has been changed in later versions to state
272    that transparency values of 1 mean 100% opacity. Documents created by
273    different tools at different times adhere to different versions of the
274    standard. This function looks at all transparency values in the database and
275    heuristically decides which way the values should be interpreted.*/
276    bool findInvertTransparency(daeDatabase*) const;
277
278    osgAnimation::BasicAnimationManager* processAnimationLibraries(domCOLLADA* document);
279    void processAnimationClip(osgAnimation::BasicAnimationManager* pOsgAnimationManager, domAnimation_clip* pDomAnimationClip);
280    void processAnimationMap(const TargetChannelPartMap&, osgAnimation::Animation* pOsgAnimation);
281    ChannelPart* processSampler(domChannel* pDomChannel, SourceMap &sources);
282    void processAnimationChannels(domAnimation* pDomAnimation, TargetChannelPartMap& tcm);
283    void processChannel(domChannel* pDomChannel, SourceMap &sources, TargetChannelPartMap& tcm);
284    void extractTargetName(const std::string&, std::string&, std::string&, std::string&);
285
286    // Processing of OSG specific info stored in node extras
287    osg::Group* processExtras(domNode *node);
288    void processNodeExtra(osg::Node* osgNode, domNode *node);
289    domTechnique* getOpenSceneGraphProfile(domExtra* extra);
290    void processAsset( domAsset *node );
291
292    osg::Group* processOsgSwitch(domTechnique* teq);
293    osg::Group* processOsgMultiSwitch(domTechnique* teq);
294    osg::Group* processOsgLOD(domTechnique* teq);
295    osg::Group* processOsgDOFTransform(domTechnique* teq);
296    osg::Group* processOsgSequence(domTechnique* teq);
297
298    // geometry processing
299    osg::Geode* getOrCreateGeometry(domGeometry *geom, domBind_material* pDomBindMaterial, const osg::Geode** ppOriginalGeode = NULL);
300    osgAnimation::Bone* getOrCreateBone(domNode *pDomNode);
301    osgAnimation::Skeleton* getOrCreateSkeleton(domNode *pDomNode);
302    osg::Geode* processInstanceGeometry( domInstance_geometry *ig );
303
304    osg::Geode* processMesh(domMesh* pDomMesh);
305    osg::Geode* processConvexMesh(domConvex_mesh* pDomConvexMesh);
306    osg::Geode* processSpline(domSpline* pDomSpline);
307    osg::Geode* processGeometry(domGeometry *pDomGeometry);
308
309    typedef std::vector<domInstance_controller*> domInstance_controllerList;
310
311    void processSkins();
312    //Process skins attached to one skeleton
313    void processSkeletonSkins(domNode* skeletonRoot, const domInstance_controllerList&);
314    void processSkin(domSkin* pDomSkin, domNode* skeletonRoot, osgAnimation::Skeleton*, domBind_material* pDomBindMaterial);
315    osg::Node* processMorph(domMorph* pDomMorph, domBind_material* pDomBindMaterial);
316    osg::Node* processInstanceController( domInstance_controller *ictrl );
317
318    template< typename T >
319    void processSinglePPrimitive(osg::Geode* geode, const domMesh* pDomMesh, const T* group, SourceMap& sources, GLenum mode);
320
321    template< typename T >
322    void processMultiPPrimitive(osg::Geode* geode, const domMesh* pDomMesh, const T* group, SourceMap& sources, GLenum mode);
323
324    void processPolylist(osg::Geode* geode, const domMesh* pDomMesh, const domPolylist *group, SourceMap &sources, TessellateMode tessellateMode);
325
326    template< typename T >
327    void processPolygons(osg::Geode* geode, const domMesh* pDomMesh, const T *group, SourceMap &sources, GLenum mode, TessellateMode tessellateMode);
328
329    void resolveMeshArrays(const domP_Array&,
330        const domInputLocalOffset_Array& inputs, const domMesh* pDomMesh,
331        osg::Geometry* geometry, SourceMap &sources,
332        std::vector<std::vector<GLuint> >& vertexLists);
333
334    //material/effect processing
335    void processBindMaterial( domBind_material *bm, domGeometry *geom, osg::Geode *geode, osg::Geode *cachedGeode );
336    void processMaterial(osg::StateSet *ss, domMaterial *mat );
337    void processEffect(osg::StateSet *ss, domEffect *effect );
338    void processProfileCOMMON(osg::StateSet *ss, domProfile_COMMON *pc );
339    bool processColorOrTextureType(const osg::StateSet*,
340                                    domCommon_color_or_texture_type *cot,
341                                    osg::Material::ColorMode channel,
342                                    osg::Material *mat,
343                                    domCommon_float_or_param_type *fop = NULL,
344                                    osg::Texture2D **sa = NULL,
345                                    bool normalizeShininess=false);
346    void processTransparencySettings( domCommon_transparent_type *ctt,
347                                        domCommon_float_or_param_type *pTransparency,
348                                        osg::StateSet*,
349                                        osg::Material *material,
350                                        unsigned int diffuseTextureUnit );
351    bool GetFloat4Param(xsNCName Reference, domFloat4 &f4) const;
352    bool GetFloatParam(xsNCName Reference, domFloat &f) const;
353
354    std::string processImagePath(const domImage*) const;
355    osg::Image* processImageTransparency(const osg::Image*, domFx_opaque_enum, float transparency) const;
356    osg::Texture2D* processTexture( domCommon_color_or_texture_type_complexType::domTexture *tex, const osg::StateSet*, TextureUnitUsage, domFx_opaque_enum = FX_OPAQUE_ENUM_A_ONE, float transparency = 1.0f);
357    bool copyTextureCoordinateSet(const osg::StateSet* ss, const osg::Geometry* cachedGeometry, osg::Geometry* clonedGeometry, const domInstance_material* im, TextureUnitUsage tuu, unsigned int textureUnit);
358
359    //scene objects
360    osg::Node* processLight( domLight *dlight );
361    osg::Node* processCamera( domCamera *dcamera );
362
363    domNode* getRootJoint(domNode*) const;
364    domNode* findJointNode(daeElement* searchFrom, domInstance_controller*) const;
365    domNode* findSkeletonNode(daeElement* searchFrom, domInstance_controller*) const;
366
367    /// Return whether the node is used as a bone. Note that while many files
368    /// identify joints with type="JOINT", some don't do this, while others
369    /// incorrectly identify every node as a joint.
370    bool isJoint(const domNode* node) const {return _jointSet.find(node) != _jointSet.end();}
371
372private:
373
374    DAE *_dae;
375    osg::Node* _rootNode;
376    osg::ref_ptr<osg::StateSet> _rootStateSet;
377    domCOLLADA* _document;
378    domVisual_scene* _visualScene;
379
380    std::map<std::string,bool> _targetMap;
381
382    int _numlights;
383
384    domInstance_effect *_currentInstance_effect;
385    domEffect *_currentEffect;
386
387    /// Maps an animated element to a domchannel to quickly find which animation influence this element
388    // TODO a single element can be animated by multiple channels (with different members like translate.x or morphweights(2) )
389    daeElementDomChannelMap _daeElementDomChannelMap;
390    /// Maps a domchannel to an animationupdatecallback
391    domChannelOsgAnimationUpdateCallbackMap _domChannelOsgAnimationUpdateCallbackMap;
392    /// Maps geometry to a Geode
393    domGeometryGeodeMap _geometryMap;
394    /// All nodes in the document that are used as joints.
395    std::set<const domNode*> _jointSet;
396    /// Maps a node (of type joint) to a osgAnimation::Bone
397    domNodeOsgBoneMap _jointMap;
398    /// Maps a node (of type joint) to a osgAnimation::Skeleton
399    domNodeOsgSkeletonMap _skeletonMap;
400    // Maps material target to stateset
401    domMaterialStateSetMap _materialMap;
402    // Maps material symbol to stateset
403    MaterialStateSetMap _materialMap2;
404    TextureParametersMap _textureParamMap;
405    TextureToCoordSetMap _texCoordSetMap;
406    domInstance_controllerList _skinInstanceControllers;
407    OldToNewIndexMap _oldToNewIndexMap;
408
409    AuthoringTool _authoringTool;
410    bool _invertTransparency;
411    Options _pluginOptions;
412
413    // Additional Information
414    std::string _assetUnitName;
415    float _assetUnitMeter;
416    domUpAxisType _assetUp_axis;
417};
418
419}
420
421#endif
422
Note: See TracBrowser for help on using the browser.