Big patches:
[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                 int i=0;
181         
182                 if (blenderscene->world)
183                 {
184                         switch (blenderscene->world->physicsEngine)
185                         {
186                                 
187                                 case WOPHY_ODE:
188                                 {
189                                         physics_engine = UseODE;
190                                         break;
191                                 }
192                                 case WOPHY_DYNAMO:
193                                 {
194                                         physics_engine = UseDynamo;
195                                         break;
196                                 }
197                                 case WOPHY_SUMO:
198                                 {
199                                         physics_engine = UseSumo;
200                                         break;
201                                 }
202                                 case WOPHY_NONE:
203                                 {
204                                         physics_engine = UseNone;
205                                 }
206                         }
207                   
208                 }
209         }
210
211         switch (physics_engine)
212         {
213         
214 #ifdef USE_SUMO_SOLID
215                 case UseSumo:
216                         destinationscene ->SetPhysicsEnvironment(new SumoPhysicsEnvironment());
217                         break;
218 #endif
219 #ifdef USE_ODE
220
221                 case UseODE:
222                         destinationscene ->SetPhysicsEnvironment(new ODEPhysicsEnvironment());
223                         break;
224 #endif //USE_ODE
225         
226                 case UseDynamo:
227                 {
228                 }
229                 
230                 default:
231                 case UseNone:
232                         physics_engine = UseNone;
233                         destinationscene ->SetPhysicsEnvironment(new DummyPhysicsEnvironment());
234                         break;
235         }
236
237         BL_ConvertBlenderObjects(m_maggie,
238                 scenename,
239                 destinationscene,
240                 m_ketsjiEngine,
241                 physics_engine,
242                 dictobj,
243                 keyinputdev,
244                 rendertools,
245                 canvas,
246                 this,
247                 m_alwaysUseExpandFraming
248                 );
249
250         m_map_blender_to_gameactuator.clear();
251         m_map_blender_to_gamecontroller.clear();
252         
253         m_map_blender_to_gameobject.clear();
254         m_map_mesh_to_gamemesh.clear();
255         m_map_gameobject_to_blender.clear();
256 }
257
258
259
260 void KX_BlenderSceneConverter::SetAlwaysUseExpandFraming(
261         bool to_what)
262 {
263         m_alwaysUseExpandFraming= to_what;
264 }
265
266         
267
268 void KX_BlenderSceneConverter::RegisterGameObject(
269                                                                         KX_GameObject *gameobject, 
270                                                                         struct Object *for_blenderobject) 
271 {
272         m_map_gameobject_to_blender.insert(CHashedPtr(gameobject),for_blenderobject);
273         m_map_blender_to_gameobject.insert(CHashedPtr(for_blenderobject),gameobject);
274 }
275
276
277
278 KX_GameObject *KX_BlenderSceneConverter::FindGameObject(
279                                                                         struct Object *for_blenderobject) 
280 {
281         KX_GameObject **obp= m_map_blender_to_gameobject[CHashedPtr(for_blenderobject)];
282         
283         return obp?*obp:NULL;
284 }
285
286
287
288 struct Object *KX_BlenderSceneConverter::FindBlenderObject(
289                                                                         KX_GameObject *for_gameobject) 
290 {
291         struct Object **obp= m_map_gameobject_to_blender[CHashedPtr(for_gameobject)];
292         
293         return obp?*obp:NULL;
294 }
295
296         
297
298 void KX_BlenderSceneConverter::RegisterGameMesh(
299                                                                         RAS_MeshObject *gamemesh,
300                                                                         struct Mesh *for_blendermesh)
301 {
302         m_map_mesh_to_gamemesh.insert(CHashedPtr(for_blendermesh),gamemesh);
303         m_meshobjects.push_back(gamemesh);
304 }
305
306
307
308 RAS_MeshObject *KX_BlenderSceneConverter::FindGameMesh(
309                                                                         struct Mesh *for_blendermesh,
310                                                                         unsigned int onlayer)
311 {
312         RAS_MeshObject** meshp = m_map_mesh_to_gamemesh[CHashedPtr(for_blendermesh)];
313         
314         if (meshp && onlayer==(*meshp)->GetLightLayer()) {
315                 return *meshp;
316         } else {
317                 return NULL;
318         }
319 }
320
321         
322
323
324         
325
326 void KX_BlenderSceneConverter::RegisterPolyMaterial(RAS_IPolyMaterial *polymat)
327 {
328         m_polymaterials.push_back(polymat);
329 }
330
331
332
333 void KX_BlenderSceneConverter::RegisterInterpolatorList(
334                                                                         BL_InterpolatorList *ipoList,
335                                                                         struct Ipo *for_ipo)
336 {
337         m_map_blender_to_gameipolist.insert(CHashedPtr(for_ipo), ipoList);
338 }
339
340
341
342 BL_InterpolatorList *KX_BlenderSceneConverter::FindInterpolatorList(
343                                                                         struct Ipo *for_ipo)
344 {
345         BL_InterpolatorList **listp = m_map_blender_to_gameipolist[CHashedPtr(for_ipo)];
346                 
347         return listp?*listp:NULL;
348 }
349
350
351
352 void KX_BlenderSceneConverter::RegisterGameActuator(
353                                                                         SCA_IActuator *act,
354                                                                         struct bActuator *for_actuator)
355 {
356         m_map_blender_to_gameactuator.insert(CHashedPtr(for_actuator), act);
357 }
358
359
360
361 SCA_IActuator *KX_BlenderSceneConverter::FindGameActuator(
362                                                                         struct bActuator *for_actuator)
363 {
364         SCA_IActuator **actp = m_map_blender_to_gameactuator[CHashedPtr(for_actuator)];
365         
366         return actp?*actp:NULL;
367 }
368
369
370
371 void KX_BlenderSceneConverter::RegisterGameController(
372                                                                         SCA_IController *cont,
373                                                                         struct bController *for_controller)
374 {
375         m_map_blender_to_gamecontroller.insert(CHashedPtr(for_controller), cont);
376 }
377
378
379
380 SCA_IController *KX_BlenderSceneConverter::FindGameController(
381                                                                         struct bController *for_controller)
382 {
383         SCA_IController **contp = m_map_blender_to_gamecontroller[CHashedPtr(for_controller)];
384         
385         return contp?*contp:NULL;
386 }
387
388
389
390 void KX_BlenderSceneConverter::RegisterWorldInfo(
391                                                                         KX_WorldInfo *worldinfo)
392 {
393         m_worldinfos.push_back(worldinfo);
394 }