I'll break this commit into two sections in the moto files
[blender-staging.git] / source / gameengine / Converter / KX_BlenderSceneConverter.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30  */
31
32 #ifdef WIN32
33         #pragma warning (disable:4786) // suppress stl-MSVC debug info warning
34 #endif
35
36 #include "KX_Scene.h"
37 #include "KX_GameObject.h"
38 #include "KX_BlenderSceneConverter.h"
39 #include "KX_IpoConvert.h"
40 #include "RAS_MeshObject.h"
41 #include "KX_PhysicsEngineEnums.h"
42
43 #include "DummyPhysicsEnvironment.h"
44
45 //to decide to use sumo/ode or dummy physics - defines USE_ODE
46 #include "KX_ConvertPhysicsObject.h"
47
48 #ifdef USE_ODE
49 #include "OdePhysicsEnvironment.h"
50 #endif //USE_ODE
51
52 #ifdef USE_SUMO_SOLID
53 #include "SumoPhysicsEnvironment.h"
54 #endif
55
56 #include "KX_BlenderSceneConverter.h"
57 #include "KX_BlenderScalarInterpolator.h"
58 #include "BL_BlenderDataConversion.h"
59 #include "BlenderWorldInfo.h"
60 #include "KX_Scene.h"
61
62 /* This little block needed for linking to Blender... */
63 #ifdef WIN32
64 #include "BLI_winstuff.h"
65 #endif
66
67 /* This list includes only data type definitions */
68 #include "DNA_scene_types.h"
69 #include "DNA_world_types.h"
70 #include "BKE_main.h"
71
72
73 KX_BlenderSceneConverter::KX_BlenderSceneConverter(
74                                                         struct Main* maggie,
75                                                         class KX_KetsjiEngine* engine
76                                                         )
77                                                         : m_maggie(maggie),
78                                                         m_ketsjiEngine(engine),
79                                                         m_alwaysUseExpandFraming(false)
80 {
81         m_newfilename = "";
82 }
83
84
85 KX_BlenderSceneConverter::~KX_BlenderSceneConverter()
86 {
87         // clears meshes, and hashmaps from blender to gameengine data
88         int i;
89         // delete sumoshapes
90         
91
92         int numipolists = m_map_blender_to_gameipolist.size();
93         for (i=0; i<numipolists; i++) {
94                 BL_InterpolatorList *ipoList= *m_map_blender_to_gameipolist.at(i);
95
96                 delete (ipoList);
97         }
98
99         vector<KX_WorldInfo*>::iterator itw = m_worldinfos.begin();
100         while (itw != m_worldinfos.end()) {
101                 delete (*itw);
102                 itw++;
103         }
104
105         vector<RAS_IPolyMaterial*>::iterator itp = m_polymaterials.begin();
106         while (itp != m_polymaterials.end()) {
107                 delete (*itp);
108                 itp++;
109         }
110         
111         vector<RAS_MeshObject*>::iterator itm = m_meshobjects.begin();
112         while (itm != m_meshobjects.end()) {
113                 delete (*itm);
114                 itm++;
115         }
116         
117 #ifdef USE_SUMO_SOLID
118         KX_ClearSumoSharedShapes();
119 #endif
120 }
121
122
123
124 void KX_BlenderSceneConverter::SetNewFileName(const STR_String& filename)
125 {
126         m_newfilename = filename;
127 }
128
129
130
131 bool KX_BlenderSceneConverter::TryAndLoadNewFile()
132 {
133         bool result = false;
134
135         // find the file
136 /*      if ()
137         {
138                 result = true;
139         }
140         // if not, clear the newfilename
141         else
142         {
143                 m_newfilename = "";     
144         }
145 */
146         return result;
147 }
148
149
150
151         /**
152          * Find the specified scene by name, or the first
153          * scene if nothing matches (shouldn't happen).
154          */
155 static struct Scene *GetSceneForName2(struct Main *maggie, const STR_String& scenename) {
156         Scene *sce;
157
158         for (sce= (Scene*) maggie->scene.first; sce; sce= (Scene*) sce->id.next)
159                 if (scenename == (sce->id.name+2))
160                         return sce;
161
162         return (Scene*) maggie->scene.first;
163 }
164
165
166 void KX_BlenderSceneConverter::ConvertScene(const STR_String& scenename,
167                                                                                         class KX_Scene* destinationscene,
168                                                                                         PyObject* dictobj,
169                                                                                         class SCA_IInputDevice* keyinputdev,
170                                                                                         class RAS_IRenderTools* rendertools,
171                                                                                         class RAS_ICanvas* canvas)
172 {
173         //find out which physics engine
174         Scene *blenderscene = GetSceneForName2(m_maggie, scenename);
175
176         e_PhysicsEngine physics_engine = UseSumo;
177
178         if (blenderscene)
179         {
180         
181                 if (blenderscene->world)
182                 {
183                         switch (blenderscene->world->physicsEngine)
184                         {
185                                 
186                                 case WOPHY_ODE:
187                                 {
188                                         physics_engine = UseODE;
189                                         break;
190                                 }
191                                 case WOPHY_DYNAMO:
192                                 {
193                                         physics_engine = UseDynamo;
194                                         break;
195                                 }
196                                 case WOPHY_SUMO:
197                                 {
198                                         physics_engine = UseSumo;
199                                         break;
200                                 }
201                                 case WOPHY_NONE:
202                                 {
203                                         physics_engine = UseNone;
204                                 }
205                         }
206                   
207                 }
208         }
209
210         switch (physics_engine)
211         {
212         
213 #ifdef USE_SUMO_SOLID
214                 case UseSumo:
215                         destinationscene ->SetPhysicsEnvironment(new SumoPhysicsEnvironment());
216                         break;
217 #endif
218 #ifdef USE_ODE
219
220                 case UseODE:
221                         destinationscene ->SetPhysicsEnvironment(new ODEPhysicsEnvironment());
222                         break;
223 #endif //USE_ODE
224         
225                 case UseDynamo:
226                 {
227                 }
228                 
229                 default:
230                 case UseNone:
231                         physics_engine = UseNone;
232                         destinationscene ->SetPhysicsEnvironment(new DummyPhysicsEnvironment());
233                         break;
234         }
235
236         BL_ConvertBlenderObjects(m_maggie,
237                 scenename,
238                 destinationscene,
239                 m_ketsjiEngine,
240                 physics_engine,
241                 dictobj,
242                 keyinputdev,
243                 rendertools,
244                 canvas,
245                 this,
246                 m_alwaysUseExpandFraming
247                 );
248
249         m_map_blender_to_gameactuator.clear();
250         m_map_blender_to_gamecontroller.clear();
251         
252         m_map_blender_to_gameobject.clear();
253         m_map_mesh_to_gamemesh.clear();
254         m_map_gameobject_to_blender.clear();
255 }
256
257
258
259 void KX_BlenderSceneConverter::SetAlwaysUseExpandFraming(
260         bool to_what)
261 {
262         m_alwaysUseExpandFraming= to_what;
263 }
264
265         
266
267 void KX_BlenderSceneConverter::RegisterGameObject(
268                                                                         KX_GameObject *gameobject, 
269                                                                         struct Object *for_blenderobject) 
270 {
271         m_map_gameobject_to_blender.insert(CHashedPtr(gameobject),for_blenderobject);
272         m_map_blender_to_gameobject.insert(CHashedPtr(for_blenderobject),gameobject);
273 }
274
275
276
277 KX_GameObject *KX_BlenderSceneConverter::FindGameObject(
278                                                                         struct Object *for_blenderobject) 
279 {
280         KX_GameObject **obp= m_map_blender_to_gameobject[CHashedPtr(for_blenderobject)];
281         
282         return obp?*obp:NULL;
283 }
284
285
286
287 struct Object *KX_BlenderSceneConverter::FindBlenderObject(
288                                                                         KX_GameObject *for_gameobject) 
289 {
290         struct Object **obp= m_map_gameobject_to_blender[CHashedPtr(for_gameobject)];
291         
292         return obp?*obp:NULL;
293 }
294
295         
296
297 void KX_BlenderSceneConverter::RegisterGameMesh(
298                                                                         RAS_MeshObject *gamemesh,
299                                                                         struct Mesh *for_blendermesh)
300 {
301         m_map_mesh_to_gamemesh.insert(CHashedPtr(for_blendermesh),gamemesh);
302         m_meshobjects.push_back(gamemesh);
303 }
304
305
306
307 RAS_MeshObject *KX_BlenderSceneConverter::FindGameMesh(
308                                                                         struct Mesh *for_blendermesh,
309                                                                         unsigned int onlayer)
310 {
311         RAS_MeshObject** meshp = m_map_mesh_to_gamemesh[CHashedPtr(for_blendermesh)];
312         
313         if (meshp && onlayer==(*meshp)->GetLightLayer()) {
314                 return *meshp;
315         } else {
316                 return NULL;
317         }
318 }
319
320         
321
322
323         
324
325 void KX_BlenderSceneConverter::RegisterPolyMaterial(RAS_IPolyMaterial *polymat)
326 {
327         m_polymaterials.push_back(polymat);
328 }
329
330
331
332 void KX_BlenderSceneConverter::RegisterInterpolatorList(
333                                                                         BL_InterpolatorList *ipoList,
334                                                                         struct Ipo *for_ipo)
335 {
336         m_map_blender_to_gameipolist.insert(CHashedPtr(for_ipo), ipoList);
337 }
338
339
340
341 BL_InterpolatorList *KX_BlenderSceneConverter::FindInterpolatorList(
342                                                                         struct Ipo *for_ipo)
343 {
344         BL_InterpolatorList **listp = m_map_blender_to_gameipolist[CHashedPtr(for_ipo)];
345                 
346         return listp?*listp:NULL;
347 }
348
349
350
351 void KX_BlenderSceneConverter::RegisterGameActuator(
352                                                                         SCA_IActuator *act,
353                                                                         struct bActuator *for_actuator)
354 {
355         m_map_blender_to_gameactuator.insert(CHashedPtr(for_actuator), act);
356 }
357
358
359
360 SCA_IActuator *KX_BlenderSceneConverter::FindGameActuator(
361                                                                         struct bActuator *for_actuator)
362 {
363         SCA_IActuator **actp = m_map_blender_to_gameactuator[CHashedPtr(for_actuator)];
364         
365         return actp?*actp:NULL;
366 }
367
368
369
370 void KX_BlenderSceneConverter::RegisterGameController(
371                                                                         SCA_IController *cont,
372                                                                         struct bController *for_controller)
373 {
374         m_map_blender_to_gamecontroller.insert(CHashedPtr(for_controller), cont);
375 }
376
377
378
379 SCA_IController *KX_BlenderSceneConverter::FindGameController(
380                                                                         struct bController *for_controller)
381 {
382         SCA_IController **contp = m_map_blender_to_gamecontroller[CHashedPtr(for_controller)];
383         
384         return contp?*contp:NULL;
385 }
386
387
388
389 void KX_BlenderSceneConverter::RegisterWorldInfo(
390                                                                         KX_WorldInfo *worldinfo)
391 {
392         m_worldinfos.push_back(worldinfo);
393 }