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

« back to all changes in this revision

Viewing changes to dae23ds_lib3ds/src/DAE23dsSceneGraphHandler.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) 2010 yaoyansi<yaoyansi2@yahoo.com.cn>
 
3
 
 
4
        This file is part of dae23ds_lib3ds.
 
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 "DAE23dsStableHeaders.h"
 
12
#include "DAE23dsSceneGraphHandler.h"
 
13
//#include "DAE23dsMeshBase.h"
 
14
#include <lib3ds.h>
 
15
 
 
16
namespace DAE23ds
 
17
{
 
18
        //------------------------------
 
19
        SceneGraphHandler::SceneGraphHandler( const COLLADAFW::VisualScene* visualScene )
 
20
                : mVisualScene( visualScene )
 
21
        {
 
22
        }
 
23
 
 
24
        //------------------------------
 
25
        SceneGraphHandler::~SceneGraphHandler()
 
26
        {
 
27
        }       
 
28
 
 
29
        //------------------------------
 
30
        bool SceneGraphHandler::handle()
 
31
        {
 
32
                NodeInfo nodeInfo( COLLADABU::Math::Matrix4::IDENTITY );
 
33
                mNodeInfoStack.push( nodeInfo );
 
34
 
 
35
                handleNodes( mVisualScene->getRootNodes());
 
36
 
 
37
                return true;
 
38
        }
 
39
 
 
40
        //------------------------------
 
41
        bool SceneGraphHandler::handleNodes( const COLLADAFW::NodePointerArray& nodesToWriter)
 
42
        {
 
43
                for ( size_t i = 0, count = nodesToWriter.getCount(); i < count; ++i)
 
44
                {
 
45
                        handleNode(nodesToWriter[i]);
 
46
                }
 
47
                return true;
 
48
        }
 
49
 
 
50
        //------------------------------
 
51
        bool SceneGraphHandler::handleNode(const COLLADAFW::Node* nodeToWriter)
 
52
        {
 
53
                const NodeInfo& parentNodeInfo = mNodeInfoStack.top();
 
54
                const COLLADABU::Math::Matrix4& parentWorldMatrix = parentNodeInfo.worldTransformation;
 
55
                COLLADABU::Math::Matrix4 worldMatrix = parentWorldMatrix * nodeToWriter->getTransformationMatrix();
 
56
                NodeInfo nodeInfo( worldMatrix );
 
57
                mNodeInfoStack.push(nodeInfo);
 
58
 
 
59
                handleInstanceGeometries( nodeToWriter, worldMatrix );
 
60
                handleInstanceLights( nodeToWriter, worldMatrix );
 
61
 
 
62
                handleNodes(nodeToWriter->getChildNodes());
 
63
 
 
64
                handleInstanceNodes( nodeToWriter->getInstanceNodes() );
 
65
 
 
66
                mNodeInfoStack.pop();
 
67
                return true;
 
68
        }
 
69
 
 
70
        //------------------------------
 
71
        void SceneGraphHandler::handleInstanceGeometries( const COLLADAFW::Node* node, const COLLADABU::Math::Matrix4& matrix )
 
72
        {
 
73
                const COLLADAFW::InstanceGeometryPointerArray& instanceGeometries = node->getInstanceGeometries();
 
74
                for ( size_t i = 0, count = instanceGeometries.getCount(); i < count; ++i)
 
75
                {
 
76
                        COLLADAFW::InstanceGeometry* instanceGeometry = instanceGeometries[i];
 
77
 
 
78
                        Writer::InstanceGeometryInfo instanceGeometryInfo( instanceGeometry, matrix/*, getNextInstanceNumber(instanceGeometry) */);
 
79
 
 
80
                        const COLLADAFW::UniqueId& meshUniqueId = instanceGeometry->getInstanciatedObjectId();
 
81
 
 
82
//                      std::cout <<"----------------------------------------------"<<std::endl;
 
83
//                      std::cout <<"instanceGeometry(name="<<instanceGeometry->getName()
 
84
//                              <<",objid="<<instanceGeometry->getObjectId()
 
85
//                              <<",clsid="<<instanceGeometry->getClassId()
 
86
//                              <<",uid="<<instanceGeometry->getUniqueId().toAscii()
 
87
//                              <<",iobjid="<<instanceGeometry->getInstanciatedObjectId().toAscii()
 
88
//                              <<std::endl;
 
89
//                      std::cout<<"meshdata(uid="<<meshdata.meshUniqueId.getClassId()<<","
 
90
//                              <<meshdata.meshUniqueId.getObjectId()<<","
 
91
//                              <<meshdata.meshUniqueId.toAscii()<<","
 
92
//                              <<meshdata.name<<","
 
93
//                              <<std::endl;
 
94
 
 
95
                        const COLLADAFW::MaterialBindingArray & mba = instanceGeometry->getMaterialBindings();
 
96
                        for(size_t i=0; i<mba.getCount(); ++i)
 
97
                        {
 
98
                                std::cout <<"material id="<<mba[i].getMaterialId()<<","
 
99
                                        <<"name="<<mba[i].getName()<<","
 
100
                                        <<"ReferencedMat="<<mba[i].getReferencedMaterial().toAscii()
 
101
                                        <<std::endl;
 
102
                        }
 
103
 
 
104
                        addInstanceGeometryInstanceGeometryInfoPair(instanceGeometry->getInstanciatedObjectId(), instanceGeometryInfo);
 
105
                }
 
106
        }
 
107
 
 
108
        //------------------------------
 
109
        void SceneGraphHandler::handleInstanceNodes( const COLLADAFW::InstanceNodePointerArray& instanceNodes)
 
110
        {
 
111
                std::cout <<"SceneGraphHandler::handleInstanceNodes(size="<<instanceNodes.getCount()<<")"<<std::endl;
 
112
 
 
113
                for ( size_t i = 0, count = instanceNodes.getCount(); i < count; ++i)
 
114
                {
 
115
                        const COLLADAFW::InstanceNode* instanceNode = instanceNodes[i];
 
116
                        const COLLADAFW::UniqueId& referencedNodeUniqueId = instanceNode->getInstanciatedObjectId();
 
117
                        
 
118
                        std::cout <<"instance node(name="<<instanceNode->getName()
 
119
                                <<",uid="<<instanceNode->getUniqueId().toAscii()
 
120
                                <<",oid="<<instanceNode->getObjectId()
 
121
                                <<",iid="<<instanceNode->getInstanciatedObjectId().toAscii()
 
122
                                <<std::endl;
 
123
 
 
124
 
 
125
                        Writer::UniqueIdNodeMap::const_iterator it = Writer::getInstance()->getUniqueIdNodeMap().find( referencedNodeUniqueId );
 
126
                        if ( it != Writer::getInstance()->getUniqueIdNodeMap().end() )
 
127
                        {
 
128
                                const COLLADAFW::Node* referencedNode = it->second;
 
129
                                handleNode( referencedNode );
 
130
                        }
 
131
                }
 
132
        }
 
133
 
 
134
        //------------------------------------
 
135
        void SceneGraphHandler::handleInstanceLights( const COLLADAFW::Node* node, const COLLADABU::Math::Matrix4& matrix )
 
136
        {
 
137
                const COLLADAFW::InstanceLightPointerArray& instanceLights = node->getInstanceLights();
 
138
                for ( size_t i = 0, count = instanceLights.getCount(); i < count; ++i)
 
139
                {
 
140
                        COLLADAFW::InstanceLight* instanceLight = instanceLights[i];
 
141
 
 
142
                        const COLLADAFW::UniqueId& uid = instanceLight->getInstanciatedObjectId();
 
143
 
 
144
                        std::cout <<"----------------------------------------------"<<std::endl;
 
145
                        std::cout<<"light uid="<< uid.toAscii()<<std::endl;
 
146
 
 
147
                        std::map<COLLADAFW::UniqueId, const COLLADAFW::Light*>::const_iterator it
 
148
                                 = Writer::getInstance()->lightMap.find(uid);
 
149
                        if(it==Writer::getInstance()->lightMap.end())//not found
 
150
                                continue;
 
151
 
 
152
 
 
153
                        const COLLADAFW::Light *light = it->second;
 
154
                        std::cout<<"light name="<< light->getName()<<std::endl;
 
155
                        std::cout<<"light type="<< light->getLightType()<<std::endl;
 
156
                        std::cout<<"light color="<< light->getColor().getRed()<<","<< light->getColor().getGreen()<<","<<light->getColor().getBlue()<<std::endl;
 
157
                        std::cout<<"light const attenuation="<< light->getConstantAttenuation().getValue()<<std::endl;
 
158
                        std::cout<<"light linear attenuation="<< light->getLinearAttenuation().getValue()<<std::endl;
 
159
                        std::cout<<"light quadratic attenuation="<< light->getQuadraticAttenuation().getValue()<<std::endl;
 
160
                        std::cout<<"light fallOffAngle="<< light->getFallOffAngle().getValue()<<std::endl;
 
161
                        std::cout<<"light fallOffExp="<< light->getFallOffExponent().getValue()<<std::endl;
 
162
 
 
163
                        COLLADABU::Math::Vector3 translation(matrix.getTrans());
 
164
                        //----------------------------------------------
 
165
                        Lib3dsLight *_3dslight = lib3ds_light_new(light->getName().c_str());
 
166
                        {
 
167
                                //_3dslight->object_flags=
 
168
                                _3dslight->spot_light = (light->getLightType()==COLLADAFW::Light::SPOT_LIGHT)?true:false;
 
169
                                //_3dslight->see_cone=
 
170
                                _3dslight->color[0]=light->getColor().getRed();
 
171
                                _3dslight->color[1]=light->getColor().getGreen();
 
172
                                _3dslight->color[2]=light->getColor().getBlue();
 
173
 
 
174
                                //_3dslight->position[0]=_3dslight->position[1]=_3dslight->position[2]=0.0;
 
175
                                _3dslight->position[0]=translation.x;
 
176
                                _3dslight->position[1]=translation.y;
 
177
                                _3dslight->position[2]=translation.z;
 
178
                                
 
179
                                _3dslight->target[0]=_3dslight->target[1]=_3dslight->target[2]=0.0;
 
180
                                //_3dslight->roll
 
181
                                //_3dslight->off
 
182
                                //_3dslight->outer_range
 
183
                                //_3dslight->inner_range
 
184
                                //_3dslight->multiplier
 
185
                                _3dslight->attenuation = light->getLinearAttenuation().getValue();
 
186
                                //_3dslight->rectangular_spot =
 
187
                                //_3dslight->shadowed
 
188
                                //_3dslight->shadow_bias
 
189
                                //_3dslight->shadow_filter
 
190
                                //_3dslight->shadow_size
 
191
                                _3dslight->spot_aspect = light->getFallOffAngle().getValue();
 
192
                                //_3dslight->use_projector
 
193
                                //_3dslight->projector
 
194
                                //_3dslight->spot_overshoot
 
195
                                //_3dslight->ray_shadows
 
196
                                //_3dslight->ray_bias
 
197
                                //_3dslight->hotspot=
 
198
                                _3dslight->falloff = light->getFallOffExponent().getValue();
 
199
                        }
 
200
 
 
201
                        //----------------------------------------------
 
202
                        if(light->getLightType()==COLLADAFW::Light::SPOT_LIGHT)
 
203
                        {
 
204
                                Lib3dsSpotlightNode* node = lib3ds_node_new_spotlight(_3dslight);
 
205
                                for(size_t i=0; i<4; ++i) 
 
206
                                {
 
207
                                        for(size_t j=0; j<4; ++j) 
 
208
                                        {
 
209
                                                node->base.matrix[i][j] = matrix.getElement(i,j);
 
210
                                                std::cout << node->base.matrix[i][j]<<", ";
 
211
                                        }
 
212
                                        std::cout <<std::endl;
 
213
                                }
 
214
                                lib3ds_file_insert_light(Writer::getInstance()->get3dsFile(), _3dslight, -1);
 
215
                        }
 
216
                        else if(light->getLightType()==COLLADAFW::Light::POINT_LIGHT)
 
217
                        {
 
218
                                Lib3dsOmnilightNode*  node = lib3ds_node_new_omnilight(_3dslight);
 
219
                                for(size_t i=0; i<4; ++i) 
 
220
                                {
 
221
                                        for(size_t j=0; j<4; ++j) 
 
222
                                        {
 
223
                                                node->base.matrix[i][j] = matrix.getElement(i,j);
 
224
                                                std::cout << node->base.matrix[i][j]<<", ";
 
225
                                        }
 
226
                                        std::cout <<std::endl;
 
227
                                }
 
228
                                lib3ds_file_insert_light(Writer::getInstance()->get3dsFile(), _3dslight, -1);
 
229
                        }else{
 
230
                                lib3ds_file_insert_light(Writer::getInstance()->get3dsFile(), _3dslight, -1);
 
231
                        }
 
232
 
 
233
                }
 
234
        }
 
235
        void SceneGraphHandler::addInstanceGeometryInstanceGeometryInfoPair( const COLLADAFW::UniqueId& geoId, const Writer::InstanceGeometryInfo& instanceGeoInfo )
 
236
        {
 
237
                if ( !Writer::getInstance()->getApplyTransformationsToMeshes() )
 
238
                {
 
239
                        // if we apply transformations to meshes, we need each instance as often as it occurs
 
240
                        Writer::InstanceGeometryInfoSet& set = Writer::getInstance()->getInstanceGeometryInfoSet();
 
241
                        if ( set.count(instanceGeoInfo) != 0 )
 
242
                        {
 
243
                                return;
 
244
                        }
 
245
                        else
 
246
                        {
 
247
                                set.insert( instanceGeoInfo );
 
248
                        }
 
249
                }
 
250
 
 
251
                Writer::GeometryUniqueIdInstanceGeometryInfoMap& map 
 
252
                        = Writer::getInstance()->getGeometryUniqueIdInstanceGeometryInfoMap();
 
253
                map.insert(std::make_pair(geoId, instanceGeoInfo));
 
254
        }
 
255
 
 
256
} // namespace DAE23ds