ff22e003d75dfcaf01ea18c2f2b8301a0ddc6155
[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 HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #ifdef WIN32
37         #pragma warning (disable:4786) // suppress stl-MSVC debug info warning
38 #endif
39
40
41 #include "KX_Scene.h"
42 #include "KX_GameObject.h"
43 #include "KX_IpoConvert.h"
44 #include "RAS_MeshObject.h"
45 #include "KX_PhysicsEngineEnums.h"
46
47 #include "DummyPhysicsEnvironment.h"
48
49 //to decide to use sumo/ode or dummy physics - defines USE_ODE
50 #include "KX_ConvertPhysicsObject.h"
51
52 #ifdef USE_ODE
53 #include "OdePhysicsEnvironment.h"
54 #endif //USE_ODE
55
56 #ifdef USE_SUMO_SOLID
57 #include "SumoPhysicsEnvironment.h"
58 #endif
59
60 #include "KX_BlenderSceneConverter.h"
61 #include "KX_BlenderScalarInterpolator.h"
62 #include "BL_BlenderDataConversion.h"
63 #include "BlenderWorldInfo.h"
64 #include "KX_Scene.h"
65
66 /* This little block needed for linking to Blender... */
67 #ifdef WIN32
68 #include "BLI_winstuff.h"
69 #endif
70
71 /* This list includes only data type definitions */
72 #include "DNA_scene_types.h"
73 #include "DNA_world_types.h"
74 #include "BKE_main.h"
75
76
77 KX_BlenderSceneConverter::KX_BlenderSceneConverter(
78                                                         struct Main* maggie,
79                                                         class KX_KetsjiEngine* engine
80                                                         )
81                                                         : m_maggie(maggie),
82                                                         m_ketsjiEngine(engine),
83                                                         m_alwaysUseExpandFraming(false)
84 {
85         m_newfilename = "";
86 }
87
88
89 KX_BlenderSceneConverter::~KX_BlenderSceneConverter()
90 {
91         // clears meshes, and hashmaps from blender to gameengine data
92         int i;
93         // delete sumoshapes
94         
95
96         int numipolists = m_map_blender_to_gameipolist.size();
97         for (i=0; i<numipolists; i++) {
98                 BL_InterpolatorList *ipoList= *m_map_blender_to_gameipolist.at(i);
99
100                 delete (ipoList);
101         }
102
103         vector<KX_WorldInfo*>::iterator itw = m_worldinfos.begin();
104         while (itw != m_worldinfos.end()) {
105                 delete (*itw);
106                 itw++;
107         }
108
109         vector<RAS_IPolyMaterial*>::iterator itp = m_polymaterials.begin();
110         while (itp != m_polymaterials.end()) {
111                 delete (*itp);
112                 itp++;
113         }
114         
115         vector<RAS_MeshObject*>::iterator itm = m_meshobjects.begin();
116         while (itm != m_meshobjects.end()) {
117                 delete (*itm);
118                 itm++;
119         }
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         /* FIXME: Force physics engine = sumo.
179         This isn't really a problem - no other physics engines are available. 
180         if (blenderscene)
181         {
182                 int i=0;
183         
184                 if (blenderscene->world)
185                 {
186                         switch (blenderscene->world->physicsEngine)
187                         {
188                                 
189                                 case WOPHY_ODE:
190                                 {
191                                         physics_engine = UseODE;
192                                         break;
193                                 }
194                                 case WOPHY_DYNAMO:
195                                 {
196                                         physics_engine = UseDynamo;
197                                         break;
198                                 }
199                                 case WOPHY_SUMO:
200                                 {
201                                         physics_engine = UseSumo;
202                                         break;
203                                 }
204                                 case WOPHY_NONE:
205                                 {
206                                         physics_engine = UseNone;
207                                 }
208                         }
209                   
210                 }
211         }
212         */
213         switch (physics_engine)
214         {
215         
216 #ifdef USE_SUMO_SOLID
217                 case UseSumo:
218                         destinationscene ->SetPhysicsEnvironment(new SumoPhysicsEnvironment());
219                         break;
220 #endif
221 #ifdef USE_ODE
222                 case UseODE:
223                         destinationscene ->SetPhysicsEnvironment(new ODEPhysicsEnvironment());
224                         break;
225 #endif //USE_ODE
226         
227                 case UseDynamo:
228                 {
229                 }
230                 
231                 default:
232                 case UseNone:
233                         physics_engine = UseNone;
234                         destinationscene ->SetPhysicsEnvironment(new DummyPhysicsEnvironment());
235                         break;
236         }
237
238         BL_ConvertBlenderObjects(m_maggie,
239                 scenename,
240                 destinationscene,
241                 m_ketsjiEngine,
242                 physics_engine,
243                 dictobj,
244                 keyinputdev,
245                 rendertools,
246                 canvas,
247                 this,
248                 m_alwaysUseExpandFraming
249                 );
250
251         m_map_blender_to_gameactuator.clear();
252         m_map_blender_to_gamecontroller.clear();
253         
254         m_map_blender_to_gameobject.clear();
255         m_map_mesh_to_gamemesh.clear();
256         m_map_gameobject_to_blender.clear();
257 }
258
259
260
261 void KX_BlenderSceneConverter::SetAlwaysUseExpandFraming(
262         bool to_what)
263 {
264         m_alwaysUseExpandFraming= to_what;
265 }
266
267         
268
269 void KX_BlenderSceneConverter::RegisterGameObject(
270                                                                         KX_GameObject *gameobject, 
271                                                                         struct Object *for_blenderobject) 
272 {
273         m_map_gameobject_to_blender.insert(CHashedPtr(gameobject),for_blenderobject);
274         m_map_blender_to_gameobject.insert(CHashedPtr(for_blenderobject),gameobject);
275 }
276
277
278
279 KX_GameObject *KX_BlenderSceneConverter::FindGameObject(
280                                                                         struct Object *for_blenderobject) 
281 {
282         KX_GameObject **obp= m_map_blender_to_gameobject[CHashedPtr(for_blenderobject)];
283         
284         return obp?*obp:NULL;
285 }
286
287
288
289 struct Object *KX_BlenderSceneConverter::FindBlenderObject(
290                                                                         KX_GameObject *for_gameobject) 
291 {
292         struct Object **obp= m_map_gameobject_to_blender[CHashedPtr(for_gameobject)];
293         
294         return obp?*obp:NULL;
295 }
296
297         
298
299 void KX_BlenderSceneConverter::RegisterGameMesh(
300                                                                         RAS_MeshObject *gamemesh,
301                                                                         struct Mesh *for_blendermesh)
302 {
303         m_map_mesh_to_gamemesh.insert(CHashedPtr(for_blendermesh),gamemesh);
304         m_meshobjects.push_back(gamemesh);
305 }
306
307
308
309 RAS_MeshObject *KX_BlenderSceneConverter::FindGameMesh(
310                                                                         struct Mesh *for_blendermesh,
311                                                                         unsigned int onlayer)
312 {
313         RAS_MeshObject** meshp = m_map_mesh_to_gamemesh[CHashedPtr(for_blendermesh)];
314         
315         if (meshp && onlayer==(*meshp)->GetLightLayer()) {
316                 return *meshp;
317         } else {
318                 return NULL;
319         }
320 }
321
322         
323
324
325         
326
327 void KX_BlenderSceneConverter::RegisterPolyMaterial(RAS_IPolyMaterial *polymat)
328 {
329         m_polymaterials.push_back(polymat);
330 }
331
332
333
334 void KX_BlenderSceneConverter::RegisterInterpolatorList(
335                                                                         BL_InterpolatorList *ipoList,
336                                                                         struct Ipo *for_ipo)
337 {
338         m_map_blender_to_gameipolist.insert(CHashedPtr(for_ipo), ipoList);
339 }
340
341
342
343 BL_InterpolatorList *KX_BlenderSceneConverter::FindInterpolatorList(
344                                                                         struct Ipo *for_ipo)
345 {
346         BL_InterpolatorList **listp = m_map_blender_to_gameipolist[CHashedPtr(for_ipo)];
347                 
348         return listp?*listp:NULL;
349 }
350
351
352
353 void KX_BlenderSceneConverter::RegisterGameActuator(
354                                                                         SCA_IActuator *act,
355                                                                         struct bActuator *for_actuator)
356 {
357         m_map_blender_to_gameactuator.insert(CHashedPtr(for_actuator), act);
358 }
359
360
361
362 SCA_IActuator *KX_BlenderSceneConverter::FindGameActuator(
363                                                                         struct bActuator *for_actuator)
364 {
365         SCA_IActuator **actp = m_map_blender_to_gameactuator[CHashedPtr(for_actuator)];
366         
367         return actp?*actp:NULL;
368 }
369
370
371
372 void KX_BlenderSceneConverter::RegisterGameController(
373                                                                         SCA_IController *cont,
374                                                                         struct bController *for_controller)
375 {
376         m_map_blender_to_gamecontroller.insert(CHashedPtr(for_controller), cont);
377 }
378
379
380
381 SCA_IController *KX_BlenderSceneConverter::FindGameController(
382                                                                         struct bController *for_controller)
383 {
384         SCA_IController **contp = m_map_blender_to_gamecontroller[CHashedPtr(for_controller)];
385         
386         return contp?*contp:NULL;
387 }
388
389
390
391 void KX_BlenderSceneConverter::RegisterWorldInfo(
392                                                                         KX_WorldInfo *worldinfo)
393 {
394         m_worldinfos.push_back(worldinfo);
395 }