2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
32 #ifndef __KX_SCENE_H__
33 #define __KX_SCENE_H__
36 #include "KX_PhysicsEngineEnums.h"
43 #include "CTR_HashedPtr.h"
44 #include "SG_IObject.h"
45 #include "SCA_IScene.h"
46 #include "MT_Transform.h"
48 #include "RAS_FramingManager.h"
52 #include "PyObjectPlus.h"
53 #include "RAS_2DFilterManager.h"
56 * \section Forward declarations
58 struct SM_MaterialProps;
65 class SCA_LogicManager;
66 class SCA_KeyboardManager;
67 class SCA_TimeEventManager;
68 class SCA_MouseManager;
70 class SCA_IInputDevice;
71 class NG_NetworkDeviceInterface;
72 class NG_NetworkScene;
80 class RAS_BucketManager;
81 class RAS_MaterialBucket;
82 class RAS_IPolyMaterial;
83 class RAS_IRasterizer;
84 class RAS_IRenderTools;
85 class SCA_JoystickManager;
86 class btCollisionShape;
87 class KX_BlenderSceneConverter;
88 struct KX_ClientObjectInfo;
89 class KX_ObstacleSimulation;
91 #ifdef WITH_CXX_GUARDEDALLOC
92 #include "MEM_guardedalloc.h"
96 #define IS_TAGGED(_id) ((_id) && (((ID *)_id)->flag & LIB_DOIT))
99 * The KX_Scene holds all data for an independent scene. It relates
100 * KX_Objects to the specific objects in the modules.
102 class KX_Scene : public PyObjectPlus, public SCA_IScene
107 PyObject* m_attr_dict;
108 PyObject* m_draw_call_pre;
109 PyObject* m_draw_call_post;
114 CullingInfo(int layer) : m_layer(layer) {}
118 RAS_BucketManager* m_bucketmanager;
119 CListValue* m_tempObjectList;
122 * The list of objects which have been removed during the
123 * course of one frame. They are actually destroyed in
124 * LogicEndFrame() via a call to RemoveObject().
126 CListValue* m_euthanasyobjects;
128 CListValue* m_objectlist;
129 CListValue* m_parentlist; // all 'root' parents
130 CListValue* m_lightlist;
131 CListValue* m_inactivelist; // all objects that are not in the active layer
132 CListValue* m_animatedlist; // all animated objects
134 SG_QList m_sghead; // list of nodes that needs scenegraph update
135 // the Dlist is not object that must be updated
136 // the Qlist is for objects that needs to be rescheduled
137 // for updates after udpate is over (slow parent, bone parent)
141 * The set of cameras for this scene
143 std::list<class KX_Camera*> m_cameras;
146 * The set of fonts for this scene
148 std::list<class KX_FontObject*> m_fonts;
152 * Various SCA managers used by the scene
154 SCA_LogicManager* m_logicmgr;
155 SCA_KeyboardManager* m_keyboardmgr;
156 SCA_MouseManager* m_mousemgr;
157 SCA_TimeEventManager* m_timemgr;
159 // Scene converter where many scene entities are registered
160 // Used to deregister objects that are deleted
161 class KX_BlenderSceneConverter* m_sceneConverter;
163 * physics engine abstraction
165 //e_PhysicsEngine m_physicsEngine; //who needs this ?
166 class PHY_IPhysicsEnvironment* m_physicsEnvironment;
169 * Does this scene clear the z-buffer?
171 bool m_isclearingZbuffer;
174 * The name of the scene
176 STR_String m_sceneName;
179 * stores the world-settings for a scene
181 KX_WorldInfo* m_worldinfo;
184 * \section Different scenes, linked to ketsji scene
190 NG_NetworkDeviceInterface* m_networkDeviceInterface;
191 NG_NetworkScene* m_networkScene;
194 * A temporary variable used to parent objects together on
195 * replication. Don't get confused by the name it is not
196 * the scene's root node!
201 * The active camera for the scene
203 KX_Camera* m_active_camera;
206 * Another temporary variable outstaying its welcome
207 * used in AddReplicaObject to map game objects to their
208 * replicas so pointers can be updated.
210 CTR_Map <CTR_HashedPtr, void*> m_map_gameobject_to_replica;
213 * Another temporary variable outstaying its welcome
214 * used in AddReplicaObject to keep a record of all added
215 * objects. Logic can only be updated when all objects
216 * have been updated. This stores a list of the new objects.
218 std::vector<KX_GameObject*> m_logicHierarchicalGameObjects;
221 * This temporary variable will contain the list of
222 * object that can be added during group instantiation.
223 * objects outside this list will not be added (can
224 * happen with children that are outside the group).
225 * Used in AddReplicaObject. If the list is empty, it
228 std::set<CValue*> m_groupGameObjects;
231 * Pointer to system variable passed in in constructor
232 * only used in constructor so we do not need to keep it
233 * around in this class.
236 SCA_ISystem* m_kxsystem;
239 * The execution priority of replicated object actuators?
241 int m_ueberExecutionPriority;
244 * Activity 'bubble' settings :
245 * Suspend (freeze) the entire scene.
250 * Radius in Manhattan distance of the box for activity culling.
252 float m_activity_box_radius;
255 * Toggle to enable or disable activity culling.
257 bool m_activity_culling;
260 * Toggle to enable or disable culling via DBVT broadphase of Bullet.
265 * Occlusion culling resolution
267 int m_dbvt_occlusion_res;
270 * The framing settings used by this scene
273 RAS_FrameSettings m_frame_settings;
276 * This scenes viewport into the game engine
277 * canvas.Maintained externally, initially [0,0] -> [0,0]
282 * Visibility testing functions.
284 void MarkVisible(SG_Tree *node, RAS_IRasterizer* rasty, KX_Camera*cam,int layer=0);
285 void MarkSubTreeVisible(SG_Tree *node, RAS_IRasterizer* rasty, bool visible, KX_Camera*cam,int layer=0);
286 void MarkVisible(RAS_IRasterizer* rasty, KX_GameObject* gameobj, KX_Camera*cam, int layer=0);
287 static void PhysicsCullingCallback(KX_ClientObjectInfo* objectInfo, void* cullingInfo);
289 double m_suspendedtime;
290 double m_suspendeddelta;
292 struct Scene* m_blenderScene;
294 RAS_2DFilterManager m_filtermanager;
296 KX_ObstacleSimulation* m_obstacleSimulation;
299 KX_Scene(class SCA_IInputDevice* keyboarddevice,
300 class SCA_IInputDevice* mousedevice,
301 class NG_NetworkDeviceInterface* ndi,
302 const STR_String& scenename,
304 class RAS_ICanvas* canvas);
309 RAS_BucketManager* GetBucketManager();
310 RAS_MaterialBucket* FindBucket(RAS_IPolyMaterial* polymat, bool &bucketCreated);
311 void RenderBuckets(const MT_Transform& cameratransform,
312 RAS_IRasterizer* rasty,
313 RAS_IRenderTools* rendertools);
316 * Update all transforms according to the scenegraph.
318 static bool KX_ScenegraphUpdateFunc(SG_IObject* node,void* gameobj,void* scene);
319 static bool KX_ScenegraphRescheduleFunc(SG_IObject* node,void* gameobj,void* scene);
320 void UpdateParents(double curtime);
321 void DupliGroupRecurse(CValue* gameobj, int level);
322 bool IsObjectInGroup(CValue* gameobj)
324 return (m_groupGameObjects.empty() ||
325 m_groupGameObjects.find(gameobj) != m_groupGameObjects.end());
327 SCA_IObject* AddReplicaObject(CValue* gameobj,
330 KX_GameObject* AddNodeReplicaObject(SG_IObject* node,
332 void RemoveNodeDestructObject(SG_IObject* node,
334 void RemoveObject(CValue* gameobj);
335 void DelayedRemoveObject(CValue* gameobj);
337 int NewRemoveObject(CValue* gameobj);
338 void ReplaceMesh(CValue* gameobj,
339 void* meshob, bool use_gfx, bool use_phys);
341 void AddAnimatedObject(CValue* gameobj);
342 void RemoveAnimatedObject(CValue* gameobj);
345 * \section Logic stuff
346 * Initiate an update of the logic system.
348 void LogicBeginFrame(double curtime);
349 void LogicUpdateFrame(double curtime, bool frame);
350 void UpdateAnimations(double curtime);
380 SCA_TimeEventManager*
386 std::list<class KX_FontObject*>*
390 /** Find a font in the scene by pointer. */
396 /** Add a camera to this scene. */
403 /** Camera Routines */
405 std::list<class KX_Camera*>*
410 /** Find a camera in the scene by pointer. */
416 /** Find a scene in the scene by name. */
422 /** Add a camera to this scene. */
428 /** Find the currently active camera. */
434 * Set this camera to be the active camera in the scene. If the
435 * camera is not present in the camera list, it will be added
444 * Move this camera to the end of the list so that it is rendered last.
445 * If the camera is not on the list, it will be added
453 * Activates new desired canvas width set at design time.
454 * \param width The new desired width.
457 SetCanvasDesignWidth(
461 * Activates new desired canvas height set at design time.
462 * \param width The new desired height.
465 SetCanvasDesignHeight(
469 * Returns the current desired canvas width set at design time.
470 * \return The desired width.
473 GetCanvasDesignWidth(
478 * Returns the current desired canvas height set at design time.
479 * \return The desired height.
482 GetCanvasDesignHeight(
487 * Set the framing options for this scene
492 RAS_FrameSettings & frame_settings
496 * Return a const reference to the framing
497 * type set by the above call.
498 * The contents are not guaranteed to be sensible
499 * if you don't call the above function.
508 * Store the current scene's viewport on the
509 * game engine canvas.
511 void SetSceneViewport(const RAS_Rect &viewport);
514 * Get the current scene's viewport on the
515 * game engine canvas. This maintained
516 * externally in KX_GameEngine
518 const RAS_Rect& GetSceneViewport() const;
521 * \section Accessors to different scenes of this scene
523 void SetNetworkDeviceInterface(NG_NetworkDeviceInterface* newInterface);
524 void SetNetworkScene(NG_NetworkScene *newScene);
525 void SetWorldInfo(class KX_WorldInfo* wi);
526 KX_WorldInfo* GetWorldInfo();
527 void CalculateVisibleMeshes(RAS_IRasterizer* rasty, KX_Camera *cam, int layer=0);
528 void UpdateMeshTransformations();
529 KX_Camera* GetpCamera();
530 NG_NetworkDeviceInterface* GetNetworkDeviceInterface();
531 NG_NetworkScene* GetNetworkScene();
532 KX_BlenderSceneConverter *GetSceneConverter() { return m_sceneConverter; }
535 * Replicate the logic bricks associated to this object.
538 void ReplicateLogic(class KX_GameObject* newobj);
539 static SG_Callbacks m_callbacks;
541 const STR_String& GetName();
543 // Suspend the entire scene.
546 // Resume a suspended scene.
549 // Update the activity box settings for objects in this scene, if needed.
550 void UpdateObjectActivity(void);
552 // Enable/disable activity culling.
553 void SetActivityCulling(bool b);
555 // Set the radius of the activity culling box.
556 void SetActivityCullingRadius(float f);
558 bool IsClearingZBuffer();
559 void EnableZBufferClearing(bool isclearingZbuffer);
560 // use of DBVT tree for camera culling
561 void SetDbvtCulling(bool b) { m_dbvt_culling = b; }
562 bool GetDbvtCulling() { return m_dbvt_culling; }
563 void SetDbvtOcclusionRes(int i) { m_dbvt_occlusion_res = i; }
564 int GetDbvtOcclusionRes() { return m_dbvt_occlusion_res; }
566 void SetSceneConverter(class KX_BlenderSceneConverter* sceneConverter);
568 class PHY_IPhysicsEnvironment* GetPhysicsEnvironment()
570 return m_physicsEnvironment;
573 void SetPhysicsEnvironment(class PHY_IPhysicsEnvironment* physEnv);
575 void SetGravity(const MT_Vector3& gravity);
577 short GetAnimationFPS();
580 * Sets the node tree for this scene.
582 void SetNodeTree(SG_Tree* root);
587 void Update2DFilter(std::vector<STR_String>& propNames, void* gameObj, RAS_2DFilterManager::RAS_2DFILTER_MODE filtermode, int pass, STR_String& text);
588 void Render2DFilters(RAS_ICanvas* canvas);
590 KX_ObstacleSimulation* GetObstacleSimulation() { return m_obstacleSimulation; }
593 /* --------------------------------------------------------------------- */
594 /* Python interface ---------------------------------------------------- */
595 /* --------------------------------------------------------------------- */
597 KX_PYMETHOD_DOC(KX_Scene, addObject);
598 KX_PYMETHOD_DOC(KX_Scene, end);
599 KX_PYMETHOD_DOC(KX_Scene, restart);
600 KX_PYMETHOD_DOC(KX_Scene, replace);
601 KX_PYMETHOD_DOC(KX_Scene, suspend);
602 KX_PYMETHOD_DOC(KX_Scene, resume);
603 KX_PYMETHOD_DOC(KX_Scene, get);
604 KX_PYMETHOD_DOC(KX_Scene, drawObstacleSimulation);
608 static PyObject* pyattr_get_name(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
609 static PyObject* pyattr_get_objects(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
610 static PyObject* pyattr_get_objects_inactive(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
611 static PyObject* pyattr_get_lights(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
612 static PyObject* pyattr_get_cameras(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
613 static PyObject* pyattr_get_active_camera(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
614 static int pyattr_set_active_camera(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
615 static PyObject* pyattr_get_drawing_callback_pre(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
616 static int pyattr_set_drawing_callback_pre(void *selv_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
617 static PyObject* pyattr_get_drawing_callback_post(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
618 static int pyattr_set_drawing_callback_post(void *selv_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
620 virtual PyObject* py_repr(void) { return PyUnicode_From_STR_String(GetName()); }
622 /* getitem/setitem */
623 static PyMappingMethods Mapping;
624 static PySequenceMethods Sequence;
627 * Run the registered python drawing functions.
629 void RunDrawingCallbacks(PyObject* cb_list);
631 PyObject* GetPreDrawCB() { return m_draw_call_pre; }
632 PyObject* GetPostDrawCB() { return m_draw_call_post; }
636 * Sets the time the scene was suspended
638 void setSuspendedTime(double suspendedtime);
640 * Returns the "curtime" the scene was suspended
642 double getSuspendedTime();
644 * Sets the difference between the local time of the scene (when it
645 * was running and not suspended) and the "curtime"
647 void setSuspendedDelta(double suspendeddelta);
649 * Returns the difference between the local time of the scene (when it
650 * was running and not suspended) and the "curtime"
652 double getSuspendedDelta();
654 * Returns the Blender scene this was made from
656 struct Scene *GetBlenderScene() { return m_blenderScene; }
658 bool MergeScene(KX_Scene *other);
661 //void PrintStats(int verbose_level) {
662 // m_bucketmanager->PrintStats(verbose_level)
666 typedef std::vector<KX_Scene*> KX_SceneList;
668 #endif //__KX_SCENE_H__