Frustum sphere culling.
[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 #ifdef USE_SUMO_SOLID
122         KX_ClearSumoSharedShapes();
123 #endif
124 }
125
126
127
128 void KX_BlenderSceneConverter::SetNewFileName(const STR_String& filename)
129 {
130         m_newfilename = filename;
131 }
132
133
134
135 bool KX_BlenderSceneConverter::TryAndLoadNewFile()
136 {
137         bool result = false;
138
139         // find the file
140 /*      if ()
141         {
142                 result = true;
143         }
144         // if not, clear the newfilename
145         else
146         {
147                 m_newfilename = "";     
148         }
149 */
150         return result;
151 }
152
153
154
155         /**
156          * Find the specified scene by name, or the first
157          * scene if nothing matches (shouldn't happen).
158          */
159 static struct Scene *GetSceneForName2(struct Main *maggie, const STR_String& scenename) {
160         Scene *sce;
161
162         for (sce= (Scene*) maggie->scene.first; sce; sce= (Scene*) sce->id.next)
163                 if (scenename == (sce->id.name+2))
164                         return sce;
165
166         return (Scene*) maggie->scene.first;
167 }
168
169
170 void KX_BlenderSceneConverter::ConvertScene(const STR_String& scenename,
171                                                                                         class KX_Scene* destinationscene,
172                                                                                         PyObject* dictobj,
173                                                                                         class SCA_IInputDevice* keyinputdev,
174                                                                                         class RAS_IRenderTools* rendertools,
175                                                                                         class RAS_ICanvas* canvas)
176 {
177         //find out which physics engine
178         Scene *blenderscene = GetSceneForName2(m_maggie, scenename);
179
180         e_PhysicsEngine physics_engine = UseSumo;
181
182         /* FIXME: Force physics engine = sumo.
183         This isn't really a problem - no other physics engines are available. 
184         if (blenderscene)
185         {
186                 int i=0;
187         
188                 if (blenderscene->world)
189                 {
190                         switch (blenderscene->world->physicsEngine)
191                         {
192                                 
193                                 case WOPHY_ODE:
194                                 {
195                                         physics_engine = UseODE;
196                                         break;
197                                 }
198                                 case WOPHY_DYNAMO:
199                                 {
200                                         physics_engine = UseDynamo;
201                                         break;
202                                 }
203                                 case WOPHY_SUMO:
204                                 {
205                                         physics_engine = UseSumo;
206                                         break;
207                                 }
208                                 case WOPHY_NONE:
209                                 {
210                                         physics_engine = UseNone;
211                                 }
212                         }
213                   
214                 }
215         }
216         */
217         switch (physics_engine)
218         {
219         
220 #ifdef USE_SUMO_SOLID
221                 case UseSumo:
222                         destinationscene ->SetPhysicsEnvironment(new SumoPhysicsEnvironment());
223                         break;
224 #endif
225 #ifdef USE_ODE
226                 case UseODE:
227                         destinationscene ->SetPhysicsEnvironment(new ODEPhysicsEnvironment());
228                         break;
229 #endif //USE_ODE
230         
231                 case UseDynamo:
232                 {
233                 }
234                 
235                 default:
236                 case UseNone:
237                         physics_engine = UseNone;
238                         destinationscene ->SetPhysicsEnvironment(new DummyPhysicsEnvironment());
239                         break;
240         }
241
242         BL_ConvertBlenderObjects(m_maggie,
243                 scenename,
244                 destinationscene,
245                 m_ketsjiEngine,
246                 physics_engine,
247                 dictobj,
248                 keyinputdev,
249                 rendertools,
250                 canvas,
251                 this,
252                 m_alwaysUseExpandFraming
253                 );
254
255         m_map_blender_to_gameactuator.clear();
256         m_map_blender_to_gamecontroller.clear();
257         
258         m_map_blender_to_gameobject.clear();
259         m_map_mesh_to_gamemesh.clear();
260         m_map_gameobject_to_blender.clear();
261 }
262
263
264
265 void KX_BlenderSceneConverter::SetAlwaysUseExpandFraming(
266         bool to_what)
267 {
268         m_alwaysUseExpandFraming= to_what;
269 }
270
271         
272
273 void KX_BlenderSceneConverter::RegisterGameObject(
274                                                                         KX_GameObject *gameobject, 
275                                                                         struct Object *for_blenderobject) 
276 {
277         m_map_gameobject_to_blender.insert(CHashedPtr(gameobject),for_blenderobject);
278         m_map_blender_to_gameobject.insert(CHashedPtr(for_blenderobject),gameobject);
279 }
280
281
282
283 KX_GameObject *KX_BlenderSceneConverter::FindGameObject(
284                                                                         struct Object *for_blenderobject) 
285 {
286         KX_GameObject **obp= m_map_blender_to_gameobject[CHashedPtr(for_blenderobject)];
287         
288         return obp?*obp:NULL;
289 }
290
291
292
293 struct Object *KX_BlenderSceneConverter::FindBlenderObject(
294                                                                         KX_GameObject *for_gameobject) 
295 {
296         struct Object **obp= m_map_gameobject_to_blender[CHashedPtr(for_gameobject)];
297         
298         return obp?*obp:NULL;
299 }
300
301         
302
303 void KX_BlenderSceneConverter::RegisterGameMesh(
304                                                                         RAS_MeshObject *gamemesh,
305                                                                         struct Mesh *for_blendermesh)
306 {
307         m_map_mesh_to_gamemesh.insert(CHashedPtr(for_blendermesh),gamemesh);
308         m_meshobjects.push_back(gamemesh);
309 }
310
311
312
313 RAS_MeshObject *KX_BlenderSceneConverter::FindGameMesh(
314                                                                         struct Mesh *for_blendermesh,
315                                                                         unsigned int onlayer)
316 {
317         RAS_MeshObject** meshp = m_map_mesh_to_gamemesh[CHashedPtr(for_blendermesh)];
318         
319         if (meshp && onlayer==(*meshp)->GetLightLayer()) {
320                 return *meshp;
321         } else {
322                 return NULL;
323         }
324 }
325
326         
327
328
329         
330
331 void KX_BlenderSceneConverter::RegisterPolyMaterial(RAS_IPolyMaterial *polymat)
332 {
333         m_polymaterials.push_back(polymat);
334 }
335
336
337
338 void KX_BlenderSceneConverter::RegisterInterpolatorList(
339                                                                         BL_InterpolatorList *ipoList,
340                                                                         struct Ipo *for_ipo)
341 {
342         m_map_blender_to_gameipolist.insert(CHashedPtr(for_ipo), ipoList);
343 }
344
345
346
347 BL_InterpolatorList *KX_BlenderSceneConverter::FindInterpolatorList(
348                                                                         struct Ipo *for_ipo)
349 {
350         BL_InterpolatorList **listp = m_map_blender_to_gameipolist[CHashedPtr(for_ipo)];
351                 
352         return listp?*listp:NULL;
353 }
354
355
356
357 void KX_BlenderSceneConverter::RegisterGameActuator(
358                                                                         SCA_IActuator *act,
359                                                                         struct bActuator *for_actuator)
360 {
361         m_map_blender_to_gameactuator.insert(CHashedPtr(for_actuator), act);
362 }
363
364
365
366 SCA_IActuator *KX_BlenderSceneConverter::FindGameActuator(
367                                                                         struct bActuator *for_actuator)
368 {
369         SCA_IActuator **actp = m_map_blender_to_gameactuator[CHashedPtr(for_actuator)];
370         
371         return actp?*actp:NULL;
372 }
373
374
375
376 void KX_BlenderSceneConverter::RegisterGameController(
377                                                                         SCA_IController *cont,
378                                                                         struct bController *for_controller)
379 {
380         m_map_blender_to_gamecontroller.insert(CHashedPtr(for_controller), cont);
381 }
382
383
384
385 SCA_IController *KX_BlenderSceneConverter::FindGameController(
386                                                                         struct bController *for_controller)
387 {
388         SCA_IController **contp = m_map_blender_to_gamecontroller[CHashedPtr(for_controller)];
389         
390         return contp?*contp:NULL;
391 }
392
393
394
395 void KX_BlenderSceneConverter::RegisterWorldInfo(
396                                                                         KX_WorldInfo *worldinfo)
397 {
398         m_worldinfos.push_back(worldinfo);
399 }