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

« back to all changes in this revision

Viewing changes to dae2ogre/src/DAE2OgreSceneGraphWriter.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) 2009 NetAllied Systems GmbH
 
3
 
 
4
This file is part of dae2ogre.
 
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 "DAE2OgreStableHeaders.h"
 
12
#include "DAE2OgreSceneGraphWriter.h"
 
13
 
 
14
 
 
15
namespace DAE2Ogre
 
16
{
 
17
 
 
18
    //------------------------------
 
19
        SceneGraphWriter::SceneGraphWriter( OgreWriter* ogreWriter, const COLLADAFW::VisualScene& visualScene, const OgreWriter::LibraryNodesList& libraryNodesList )
 
20
                : BaseWriter( ogreWriter )
 
21
            , mVisualScene(visualScene)
 
22
                , mLibraryNodesList(libraryNodesList)
 
23
        {
 
24
        }
 
25
 
 
26
    //------------------------------
 
27
        SceneGraphWriter::~SceneGraphWriter()
 
28
        {
 
29
        }
 
30
 
 
31
        //------------------------------
 
32
        bool SceneGraphWriter::write(  )
 
33
        {
 
34
                createUniqueIdNodeMap();
 
35
 
 
36
                NodeInfo nodeInfo( COLLADABU::Math::Matrix4::IDENTITY );
 
37
                mNodeInfoStack.push( nodeInfo );
 
38
 
 
39
                writeNodes( mVisualScene.getRootNodes());
 
40
                return true;
 
41
        }
 
42
 
 
43
        //------------------------------
 
44
        bool SceneGraphWriter::writeNodes( const COLLADAFW::NodePointerArray& nodesToWriter )
 
45
        {
 
46
                for ( size_t i = 0, count = nodesToWriter.getCount(); i < count; ++i)
 
47
                {
 
48
                        writeNode(nodesToWriter[i]);
 
49
                }
 
50
                return true;
 
51
        }
 
52
 
 
53
        //------------------------------
 
54
        bool SceneGraphWriter::writeNode(const COLLADAFW::Node* nodeToWriter )
 
55
        {
 
56
                
 
57
                const NodeInfo& parentNodeInfo = mNodeInfoStack.top();
 
58
                const COLLADABU::Math::Matrix4& parentWorldMatrix = parentNodeInfo.worldTransformation;
 
59
                COLLADABU::Math::Matrix4 worldMatrix = parentWorldMatrix * nodeToWriter->getTransformationMatrix();
 
60
                NodeInfo nodeInfo( worldMatrix );
 
61
                mNodeInfoStack.push(nodeInfo);
 
62
 
 
63
                writeNodes(nodeToWriter->getChildNodes());
 
64
 
 
65
                storeInstanceGeometries( nodeToWriter->getInstanceGeometries(), worldMatrix);
 
66
 
 
67
                writeInstanceNodes( nodeToWriter->getInstanceNodes() );
 
68
 
 
69
                mNodeInfoStack.pop();
 
70
 
 
71
                return true;
 
72
        }
 
73
 
 
74
 
 
75
        //------------------------------
 
76
        void SceneGraphWriter::storeInstanceGeometries( const COLLADAFW::InstanceGeometryPointerArray& instanceGeometries, 
 
77
                                                                                                        const COLLADABU::Math::Matrix4& worldMatrix  )
 
78
        {
 
79
                for ( size_t i = 0, count = instanceGeometries.getCount(); i < count; ++i)
 
80
                {
 
81
                        COLLADAFW::InstanceGeometry* instanceGeometry = instanceGeometries[i];
 
82
                        OgreWriter::InstanceGeometryInfo instanceGeometryInfo( instanceGeometry, worldMatrix );
 
83
                        addGeometryUniqueIdInstanceGeometryInfoPair(instanceGeometry->getInstanciatedObjectId(), instanceGeometryInfo);
 
84
                }
 
85
        }
 
86
 
 
87
        //------------------------------
 
88
        void SceneGraphWriter::writeInstanceNodes( const COLLADAFW::InstanceNodePointerArray& instanceNodes)
 
89
        {
 
90
                for ( size_t i = 0, count = instanceNodes.getCount(); i < count; ++i)
 
91
                {
 
92
                        const COLLADAFW::InstanceNode* instanceNode = instanceNodes[i];
 
93
                        const COLLADAFW::UniqueId& referencedNodeUniqueId = instanceNode->getInstanciatedObjectId();
 
94
                        UniqueIdNodeMap::const_iterator it = mUniqueIdNodeMap.find( referencedNodeUniqueId );
 
95
                        if ( it != mUniqueIdNodeMap.end() )
 
96
                        {
 
97
                                const COLLADAFW::Node* referencedNode = it->second;
 
98
                                writeNode( referencedNode );
 
99
                        }
 
100
                }
 
101
        }
 
102
 
 
103
        //------------------------------
 
104
        void SceneGraphWriter::createUniqueIdNodeMap( COLLADAFW::Node* node )
 
105
        {
 
106
                mUniqueIdNodeMap[node->getUniqueId()] = node;
 
107
                createUniqueIdNodeMap( node->getChildNodes() );
 
108
        }
 
109
 
 
110
        //------------------------------
 
111
        void SceneGraphWriter::createUniqueIdNodeMap( const COLLADAFW::NodePointerArray& nodes )
 
112
        {
 
113
                for ( size_t i = 0, cout = nodes.getCount(); i < cout; ++i)
 
114
                {
 
115
                        COLLADAFW::Node* node = nodes[i];
 
116
                        createUniqueIdNodeMap( node );
 
117
                }
 
118
        }
 
119
 
 
120
        //------------------------------
 
121
        void SceneGraphWriter::createUniqueIdNodeMap()
 
122
        {
 
123
                createUniqueIdNodeMap( mVisualScene.getRootNodes() );
 
124
                OgreWriter::LibraryNodesList::const_iterator it = mLibraryNodesList.begin();
 
125
                for ( ; it != mLibraryNodesList.end(); ++it )
 
126
                {
 
127
                        const COLLADAFW::LibraryNodes& libraryNodes = *it;
 
128
                        createUniqueIdNodeMap( libraryNodes.getNodes() );
 
129
                }
 
130
        }
 
131
 
 
132
} // namespace DAE2Ogre