4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
33 #include "KX_PhysicsEngineEnums.h"
40 #include "GEN_HashedPtr.h"
41 #include "SG_IObject.h"
42 #include "SCA_IScene.h"
43 #include "MT_Transform.h"
45 #include "RAS_FramingManager.h"
49 #include "PyObjectPlus.h"
50 #include "RAS_2DFilterManager.h"
53 * @section Forward declarations
55 struct SM_MaterialProps;
62 class SCA_LogicManager;
63 class SCA_KeyboardManager;
64 class SCA_TimeEventManager;
65 class SCA_MouseManager;
67 class SCA_IInputDevice;
68 class NG_NetworkDeviceInterface;
69 class NG_NetworkScene;
77 class RAS_BucketManager;
78 class RAS_MaterialBucket;
79 class RAS_IPolyMaterial;
80 class RAS_IRasterizer;
81 class RAS_IRenderTools;
82 class SCA_JoystickManager;
83 class btCollisionShape;
84 class KX_BlenderSceneConverter;
85 struct KX_ClientObjectInfo;
87 #ifdef WITH_CXX_GUARDEDALLOC
88 #include "MEM_guardedalloc.h"
92 #define IS_TAGGED(_id) ((_id) && (((ID *)_id)->flag & LIB_DOIT))
95 * The KX_Scene holds all data for an independent scene. It relates
96 * KX_Objects to the specific objects in the modules.
98 class KX_Scene : public PyObjectPlus, public SCA_IScene
102 #ifndef DISABLE_PYTHON
103 PyObject* m_attr_dict;
104 PyObject* m_draw_call_pre;
105 PyObject* m_draw_call_post;
110 CullingInfo(int layer) : m_layer(layer) {}
114 RAS_BucketManager* m_bucketmanager;
115 CListValue* m_tempObjectList;
118 * The list of objects which have been removed during the
119 * course of one frame. They are actually destroyed in
120 * LogicEndFrame() via a call to RemoveObject().
122 CListValue* m_euthanasyobjects;
124 CListValue* m_objectlist;
125 CListValue* m_parentlist; // all 'root' parents
126 CListValue* m_lightlist;
127 CListValue* m_inactivelist; // all objects that are not in the active layer
129 SG_QList m_sghead; // list of nodes that needs scenegraph update
130 // the Dlist is not object that must be updated
131 // the Qlist is for objects that needs to be rescheduled
132 // for updates after udpate is over (slow parent, bone parent)
136 * The set of cameras for this scene
138 list<class KX_Camera*> m_cameras;
140 * Various SCA managers used by the scene
142 SCA_LogicManager* m_logicmgr;
143 SCA_KeyboardManager* m_keyboardmgr;
144 SCA_MouseManager* m_mousemgr;
145 SCA_TimeEventManager* m_timemgr;
147 // Scene converter where many scene entities are registered
148 // Used to deregister objects that are deleted
149 class KX_BlenderSceneConverter* m_sceneConverter;
151 * physics engine abstraction
153 //e_PhysicsEngine m_physicsEngine; //who needs this ?
154 class PHY_IPhysicsEnvironment* m_physicsEnvironment;
157 * Does this scene clear the z-buffer?
159 bool m_isclearingZbuffer;
162 * The name of the scene
164 STR_String m_sceneName;
167 * stores the worldsettings for a scene
169 KX_WorldInfo* m_worldinfo;
172 * @section Different scenes, linked to ketsji scene
178 NG_NetworkDeviceInterface* m_networkDeviceInterface;
179 NG_NetworkScene* m_networkScene;
182 * A temoprary variable used to parent objects together on
183 * replication. Don't get confused by the name it is not
184 * the scene's root node!
189 * The active camera for the scene
191 KX_Camera* m_active_camera;
194 * Another temporary variable outstaying its welcome
195 * used in AddReplicaObject to map game objects to their
196 * replicas so pointers can be updated.
198 GEN_Map <GEN_HashedPtr, void*> m_map_gameobject_to_replica;
201 * Another temporary variable outstaying its welcome
202 * used in AddReplicaObject to keep a record of all added
203 * objects. Logic can only be updated when all objects
204 * have been updated. This stores a list of the new objects.
206 std::vector<KX_GameObject*> m_logicHierarchicalGameObjects;
209 * This temporary variable will contain the list of
210 * object that can be added during group instantiation.
211 * objects outside this list will not be added (can
212 * happen with children that are outside the group).
213 * Used in AddReplicaObject. If the list is empty, it
216 std::set<CValue*> m_groupGameObjects;
219 * Pointer to system variable passed in in constructor
220 * only used in constructor so we do not need to keep it
221 * around in this class.
224 SCA_ISystem* m_kxsystem;
227 * The execution priority of replicated object actuators?
229 int m_ueberExecutionPriority;
232 * Activity 'bubble' settings :
233 * Suspend (freeze) the entire scene.
238 * Radius in Manhattan distance of the box for activity culling.
240 float m_activity_box_radius;
243 * Toggle to enable or disable activity culling.
245 bool m_activity_culling;
248 * Toggle to enable or disable culling via DBVT broadphase of Bullet.
253 * Occlusion culling resolution
255 int m_dbvt_occlusion_res;
258 * The framing settings used by this scene
261 RAS_FrameSettings m_frame_settings;
264 * This scenes viewport into the game engine
265 * canvas.Maintained externally, initially [0,0] -> [0,0]
270 * Visibility testing functions.
272 void MarkVisible(SG_Tree *node, RAS_IRasterizer* rasty, KX_Camera*cam,int layer=0);
273 void MarkSubTreeVisible(SG_Tree *node, RAS_IRasterizer* rasty, bool visible, KX_Camera*cam,int layer=0);
274 void MarkVisible(RAS_IRasterizer* rasty, KX_GameObject* gameobj, KX_Camera*cam, int layer=0);
275 static void PhysicsCullingCallback(KX_ClientObjectInfo* objectInfo, void* cullingInfo);
277 double m_suspendedtime;
278 double m_suspendeddelta;
280 struct Scene* m_blenderScene;
282 RAS_2DFilterManager m_filtermanager;
284 KX_Scene(class SCA_IInputDevice* keyboarddevice,
285 class SCA_IInputDevice* mousedevice,
286 class NG_NetworkDeviceInterface* ndi,
287 const STR_String& scenename,
289 class RAS_ICanvas* canvas);
294 RAS_BucketManager* GetBucketManager();
295 RAS_MaterialBucket* FindBucket(RAS_IPolyMaterial* polymat, bool &bucketCreated);
296 void RenderBuckets(const MT_Transform& cameratransform,
297 RAS_IRasterizer* rasty,
298 RAS_IRenderTools* rendertools);
301 * Update all transforms according to the scenegraph.
303 static bool KX_ScenegraphUpdateFunc(SG_IObject* node,void* gameobj,void* scene);
304 static bool KX_ScenegraphRescheduleFunc(SG_IObject* node,void* gameobj,void* scene);
305 void UpdateParents(double curtime);
306 void DupliGroupRecurse(CValue* gameobj, int level);
307 bool IsObjectInGroup(CValue* gameobj)
309 return (m_groupGameObjects.empty() ||
310 m_groupGameObjects.find(gameobj) != m_groupGameObjects.end());
312 SCA_IObject* AddReplicaObject(CValue* gameobj,
315 KX_GameObject* AddNodeReplicaObject(SG_IObject* node,
317 void RemoveNodeDestructObject(SG_IObject* node,
319 void RemoveObject(CValue* gameobj);
320 void DelayedRemoveObject(CValue* gameobj);
322 int NewRemoveObject(CValue* gameobj);
323 void ReplaceMesh(CValue* gameobj,
324 void* meshob, bool use_gfx, bool use_phys);
326 * @section Logic stuff
327 * Initiate an update of the logic system.
329 void LogicBeginFrame(double curtime);
330 void LogicUpdateFrame(double curtime, bool frame);
360 SCA_TimeEventManager*
364 list<class KX_Camera*>*
369 /** Find a camera in the scene by pointer. */
375 /** Find a scene in the scene by name. */
381 /** Add a camera to this scene. */
387 /** Find the currently active camera. */
393 * Set this camera to be the active camera in the scene. If the
394 * camera is not present in the camera list, it will be added
403 * Move this camera to the end of the list so that it is rendered last.
404 * If the camera is not on the list, it will be added
412 * Activates new desired canvas width set at design time.
413 * @param width The new desired width.
416 SetCanvasDesignWidth(
420 * Activates new desired canvas height set at design time.
421 * @param width The new desired height.
424 SetCanvasDesignHeight(
428 * Returns the current desired canvas width set at design time.
429 * @return The desired width.
432 GetCanvasDesignWidth(
437 * Returns the current desired canvas height set at design time.
438 * @return The desired height.
441 GetCanvasDesignHeight(
446 * Set the framing options for this scene
451 RAS_FrameSettings & frame_settings
455 * Return a const reference to the framing
456 * type set by the above call.
457 * The contents are not guarenteed to be sensible
458 * if you don't call the above function.
467 * Store the current scene's viewport on the
468 * game engine canvas.
470 void SetSceneViewport(const RAS_Rect &viewport);
473 * Get the current scene's viewport on the
474 * game engine canvas. This maintained
475 * externally in KX_GameEngine
477 const RAS_Rect& GetSceneViewport() const;
480 * @section Accessors to different scenes of this scene
482 void SetNetworkDeviceInterface(NG_NetworkDeviceInterface* newInterface);
483 void SetNetworkScene(NG_NetworkScene *newScene);
484 void SetWorldInfo(class KX_WorldInfo* wi);
485 KX_WorldInfo* GetWorldInfo();
486 void CalculateVisibleMeshes(RAS_IRasterizer* rasty, KX_Camera *cam, int layer=0);
487 void UpdateMeshTransformations();
488 KX_Camera* GetpCamera();
489 NG_NetworkDeviceInterface* GetNetworkDeviceInterface();
490 NG_NetworkScene* GetNetworkScene();
491 KX_BlenderSceneConverter *GetSceneConverter() { return m_sceneConverter; }
494 * Replicate the logic bricks associated to this object.
497 void ReplicateLogic(class KX_GameObject* newobj);
498 static SG_Callbacks m_callbacks;
500 const STR_String& GetName();
502 // Suspend the entire scene.
505 // Resume a suspended scene.
508 // Update the activity box settings for objects in this scene, if needed.
509 void UpdateObjectActivity(void);
511 // Enable/disable activity culling.
512 void SetActivityCulling(bool b);
514 // Set the radius of the activity culling box.
515 void SetActivityCullingRadius(float f);
517 bool IsClearingZBuffer();
518 void EnableZBufferClearing(bool isclearingZbuffer);
519 // use of DBVT tree for camera culling
520 void SetDbvtCulling(bool b) { m_dbvt_culling = b; };
521 bool GetDbvtCulling() { return m_dbvt_culling; };
522 void SetDbvtOcclusionRes(int i) { m_dbvt_occlusion_res = i; };
523 int GetDbvtOcclusionRes() { return m_dbvt_occlusion_res; };
525 void SetSceneConverter(class KX_BlenderSceneConverter* sceneConverter);
527 class PHY_IPhysicsEnvironment* GetPhysicsEnvironment()
529 return m_physicsEnvironment;
532 void SetPhysicsEnvironment(class PHY_IPhysicsEnvironment* physEnv);
534 void SetGravity(const MT_Vector3& gravity);
537 * Sets the node tree for this scene.
539 void SetNodeTree(SG_Tree* root);
544 void Update2DFilter(vector<STR_String>& propNames, void* gameObj, RAS_2DFilterManager::RAS_2DFILTER_MODE filtermode, int pass, STR_String& text);
545 void Render2DFilters(RAS_ICanvas* canvas);
547 #ifndef DISABLE_PYTHON
548 /* --------------------------------------------------------------------- */
549 /* Python interface ---------------------------------------------------- */
550 /* --------------------------------------------------------------------- */
552 KX_PYMETHOD_DOC(KX_Scene, addObject);
553 KX_PYMETHOD_DOC(KX_Scene, end);
554 KX_PYMETHOD_DOC(KX_Scene, restart);
555 KX_PYMETHOD_DOC(KX_Scene, replace);
556 KX_PYMETHOD_DOC(KX_Scene, suspend);
557 KX_PYMETHOD_DOC(KX_Scene, resume);
558 KX_PYMETHOD_DOC(KX_Scene, get);
561 static PyObject* pyattr_get_name(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
562 static PyObject* pyattr_get_objects(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
563 static PyObject* pyattr_get_objects_inactive(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
564 static PyObject* pyattr_get_lights(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
565 static PyObject* pyattr_get_cameras(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
566 static PyObject* pyattr_get_active_camera(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
567 static int pyattr_set_active_camera(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
568 static PyObject* pyattr_get_drawing_callback_pre(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
569 static int pyattr_set_drawing_callback_pre(void *selv_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
570 static PyObject* pyattr_get_drawing_callback_post(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
571 static int pyattr_set_drawing_callback_post(void *selv_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
573 virtual PyObject* py_repr(void) { return PyUnicode_FromString(GetName().ReadPtr()); }
575 /* getitem/setitem */
576 static PyMappingMethods Mapping;
577 static PySequenceMethods Sequence;
580 * Run the registered python drawing functions.
582 void RunDrawingCallbacks(PyObject* cb_list);
584 PyObject* GetPreDrawCB() { return m_draw_call_pre; };
585 PyObject* GetPostDrawCB() { return m_draw_call_post; };
589 * Sets the time the scene was suspended
591 void setSuspendedTime(double suspendedtime);
593 * Returns the "curtime" the scene was suspended
595 double getSuspendedTime();
597 * Sets the difference between the local time of the scene (when it
598 * was running and not suspended) and the "curtime"
600 void setSuspendedDelta(double suspendeddelta);
602 * Returns the difference between the local time of the scene (when it
603 * was running and not suspended) and the "curtime"
605 double getSuspendedDelta();
607 * Returns the Blender scene this was made from
609 struct Scene *GetBlenderScene() { return m_blenderScene; }
611 bool MergeScene(KX_Scene *other);
614 //void PrintStats(int verbose_level) {
615 // m_bucketmanager->PrintStats(verbose_level)
619 typedef std::vector<KX_Scene*> KX_SceneList;
621 #endif //__KX_SCENE_H