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;
86 class KX_ObstacleSimulation;
88 #ifdef WITH_CXX_GUARDEDALLOC
89 #include "MEM_guardedalloc.h"
93 #define IS_TAGGED(_id) ((_id) && (((ID *)_id)->flag & LIB_DOIT))
96 * The KX_Scene holds all data for an independent scene. It relates
97 * KX_Objects to the specific objects in the modules.
99 class KX_Scene : public PyObjectPlus, public SCA_IScene
103 #ifndef DISABLE_PYTHON
104 PyObject* m_attr_dict;
105 PyObject* m_draw_call_pre;
106 PyObject* m_draw_call_post;
111 CullingInfo(int layer) : m_layer(layer) {}
115 RAS_BucketManager* m_bucketmanager;
116 CListValue* m_tempObjectList;
119 * The list of objects which have been removed during the
120 * course of one frame. They are actually destroyed in
121 * LogicEndFrame() via a call to RemoveObject().
123 CListValue* m_euthanasyobjects;
125 CListValue* m_objectlist;
126 CListValue* m_parentlist; // all 'root' parents
127 CListValue* m_lightlist;
128 CListValue* m_inactivelist; // all objects that are not in the active layer
130 SG_QList m_sghead; // list of nodes that needs scenegraph update
131 // the Dlist is not object that must be updated
132 // the Qlist is for objects that needs to be rescheduled
133 // for updates after udpate is over (slow parent, bone parent)
137 * The set of cameras for this scene
139 list<class KX_Camera*> m_cameras;
141 * Various SCA managers used by the scene
143 SCA_LogicManager* m_logicmgr;
144 SCA_KeyboardManager* m_keyboardmgr;
145 SCA_MouseManager* m_mousemgr;
146 SCA_TimeEventManager* m_timemgr;
148 // Scene converter where many scene entities are registered
149 // Used to deregister objects that are deleted
150 class KX_BlenderSceneConverter* m_sceneConverter;
152 * physics engine abstraction
154 //e_PhysicsEngine m_physicsEngine; //who needs this ?
155 class PHY_IPhysicsEnvironment* m_physicsEnvironment;
158 * Does this scene clear the z-buffer?
160 bool m_isclearingZbuffer;
163 * The name of the scene
165 STR_String m_sceneName;
168 * stores the worldsettings for a scene
170 KX_WorldInfo* m_worldinfo;
173 * @section Different scenes, linked to ketsji scene
179 NG_NetworkDeviceInterface* m_networkDeviceInterface;
180 NG_NetworkScene* m_networkScene;
183 * A temoprary variable used to parent objects together on
184 * replication. Don't get confused by the name it is not
185 * the scene's root node!
190 * The active camera for the scene
192 KX_Camera* m_active_camera;
195 * Another temporary variable outstaying its welcome
196 * used in AddReplicaObject to map game objects to their
197 * replicas so pointers can be updated.
199 GEN_Map <GEN_HashedPtr, void*> m_map_gameobject_to_replica;
202 * Another temporary variable outstaying its welcome
203 * used in AddReplicaObject to keep a record of all added
204 * objects. Logic can only be updated when all objects
205 * have been updated. This stores a list of the new objects.
207 std::vector<KX_GameObject*> m_logicHierarchicalGameObjects;
210 * This temporary variable will contain the list of
211 * object that can be added during group instantiation.
212 * objects outside this list will not be added (can
213 * happen with children that are outside the group).
214 * Used in AddReplicaObject. If the list is empty, it
217 std::set<CValue*> m_groupGameObjects;
220 * Pointer to system variable passed in in constructor
221 * only used in constructor so we do not need to keep it
222 * around in this class.
225 SCA_ISystem* m_kxsystem;
228 * The execution priority of replicated object actuators?
230 int m_ueberExecutionPriority;
233 * Activity 'bubble' settings :
234 * Suspend (freeze) the entire scene.
239 * Radius in Manhattan distance of the box for activity culling.
241 float m_activity_box_radius;
244 * Toggle to enable or disable activity culling.
246 bool m_activity_culling;
249 * Toggle to enable or disable culling via DBVT broadphase of Bullet.
254 * Occlusion culling resolution
256 int m_dbvt_occlusion_res;
259 * The framing settings used by this scene
262 RAS_FrameSettings m_frame_settings;
265 * This scenes viewport into the game engine
266 * canvas.Maintained externally, initially [0,0] -> [0,0]
271 * Visibility testing functions.
273 void MarkVisible(SG_Tree *node, RAS_IRasterizer* rasty, KX_Camera*cam,int layer=0);
274 void MarkSubTreeVisible(SG_Tree *node, RAS_IRasterizer* rasty, bool visible, KX_Camera*cam,int layer=0);
275 void MarkVisible(RAS_IRasterizer* rasty, KX_GameObject* gameobj, KX_Camera*cam, int layer=0);
276 static void PhysicsCullingCallback(KX_ClientObjectInfo* objectInfo, void* cullingInfo);
278 double m_suspendedtime;
279 double m_suspendeddelta;
281 struct Scene* m_blenderScene;
283 RAS_2DFilterManager m_filtermanager;
285 KX_ObstacleSimulation* m_obstacleSimulation;
288 KX_Scene(class SCA_IInputDevice* keyboarddevice,
289 class SCA_IInputDevice* mousedevice,
290 class NG_NetworkDeviceInterface* ndi,
291 const STR_String& scenename,
293 class RAS_ICanvas* canvas);
298 RAS_BucketManager* GetBucketManager();
299 RAS_MaterialBucket* FindBucket(RAS_IPolyMaterial* polymat, bool &bucketCreated);
300 void RenderBuckets(const MT_Transform& cameratransform,
301 RAS_IRasterizer* rasty,
302 RAS_IRenderTools* rendertools);
305 * Update all transforms according to the scenegraph.
307 static bool KX_ScenegraphUpdateFunc(SG_IObject* node,void* gameobj,void* scene);
308 static bool KX_ScenegraphRescheduleFunc(SG_IObject* node,void* gameobj,void* scene);
309 void UpdateParents(double curtime);
310 void DupliGroupRecurse(CValue* gameobj, int level);
311 bool IsObjectInGroup(CValue* gameobj)
313 return (m_groupGameObjects.empty() ||
314 m_groupGameObjects.find(gameobj) != m_groupGameObjects.end());
316 SCA_IObject* AddReplicaObject(CValue* gameobj,
319 KX_GameObject* AddNodeReplicaObject(SG_IObject* node,
321 void RemoveNodeDestructObject(SG_IObject* node,
323 void RemoveObject(CValue* gameobj);
324 void DelayedRemoveObject(CValue* gameobj);
326 int NewRemoveObject(CValue* gameobj);
327 void ReplaceMesh(CValue* gameobj,
328 void* meshob, bool use_gfx, bool use_phys);
330 * @section Logic stuff
331 * Initiate an update of the logic system.
333 void LogicBeginFrame(double curtime);
334 void LogicUpdateFrame(double curtime, bool frame);
364 SCA_TimeEventManager*
368 list<class KX_Camera*>*
373 /** Find a camera in the scene by pointer. */
379 /** Find a scene in the scene by name. */
385 /** Add a camera to this scene. */
391 /** Find the currently active camera. */
397 * Set this camera to be the active camera in the scene. If the
398 * camera is not present in the camera list, it will be added
407 * Move this camera to the end of the list so that it is rendered last.
408 * If the camera is not on the list, it will be added
416 * Activates new desired canvas width set at design time.
417 * @param width The new desired width.
420 SetCanvasDesignWidth(
424 * Activates new desired canvas height set at design time.
425 * @param width The new desired height.
428 SetCanvasDesignHeight(
432 * Returns the current desired canvas width set at design time.
433 * @return The desired width.
436 GetCanvasDesignWidth(
441 * Returns the current desired canvas height set at design time.
442 * @return The desired height.
445 GetCanvasDesignHeight(
450 * Set the framing options for this scene
455 RAS_FrameSettings & frame_settings
459 * Return a const reference to the framing
460 * type set by the above call.
461 * The contents are not guarenteed to be sensible
462 * if you don't call the above function.
471 * Store the current scene's viewport on the
472 * game engine canvas.
474 void SetSceneViewport(const RAS_Rect &viewport);
477 * Get the current scene's viewport on the
478 * game engine canvas. This maintained
479 * externally in KX_GameEngine
481 const RAS_Rect& GetSceneViewport() const;
484 * @section Accessors to different scenes of this scene
486 void SetNetworkDeviceInterface(NG_NetworkDeviceInterface* newInterface);
487 void SetNetworkScene(NG_NetworkScene *newScene);
488 void SetWorldInfo(class KX_WorldInfo* wi);
489 KX_WorldInfo* GetWorldInfo();
490 void CalculateVisibleMeshes(RAS_IRasterizer* rasty, KX_Camera *cam, int layer=0);
491 void UpdateMeshTransformations();
492 KX_Camera* GetpCamera();
493 NG_NetworkDeviceInterface* GetNetworkDeviceInterface();
494 NG_NetworkScene* GetNetworkScene();
495 KX_BlenderSceneConverter *GetSceneConverter() { return m_sceneConverter; }
498 * Replicate the logic bricks associated to this object.
501 void ReplicateLogic(class KX_GameObject* newobj);
502 static SG_Callbacks m_callbacks;
504 const STR_String& GetName();
506 // Suspend the entire scene.
509 // Resume a suspended scene.
512 // Update the activity box settings for objects in this scene, if needed.
513 void UpdateObjectActivity(void);
515 // Enable/disable activity culling.
516 void SetActivityCulling(bool b);
518 // Set the radius of the activity culling box.
519 void SetActivityCullingRadius(float f);
521 bool IsClearingZBuffer();
522 void EnableZBufferClearing(bool isclearingZbuffer);
523 // use of DBVT tree for camera culling
524 void SetDbvtCulling(bool b) { m_dbvt_culling = b; };
525 bool GetDbvtCulling() { return m_dbvt_culling; };
526 void SetDbvtOcclusionRes(int i) { m_dbvt_occlusion_res = i; };
527 int GetDbvtOcclusionRes() { return m_dbvt_occlusion_res; };
529 void SetSceneConverter(class KX_BlenderSceneConverter* sceneConverter);
531 class PHY_IPhysicsEnvironment* GetPhysicsEnvironment()
533 return m_physicsEnvironment;
536 void SetPhysicsEnvironment(class PHY_IPhysicsEnvironment* physEnv);
538 void SetGravity(const MT_Vector3& gravity);
541 * Sets the node tree for this scene.
543 void SetNodeTree(SG_Tree* root);
548 void Update2DFilter(vector<STR_String>& propNames, void* gameObj, RAS_2DFilterManager::RAS_2DFILTER_MODE filtermode, int pass, STR_String& text);
549 void Render2DFilters(RAS_ICanvas* canvas);
551 KX_ObstacleSimulation* GetObstacleSimulation() {return m_obstacleSimulation;};
553 #ifndef DISABLE_PYTHON
554 /* --------------------------------------------------------------------- */
555 /* Python interface ---------------------------------------------------- */
556 /* --------------------------------------------------------------------- */
558 KX_PYMETHOD_DOC(KX_Scene, addObject);
559 KX_PYMETHOD_DOC(KX_Scene, end);
560 KX_PYMETHOD_DOC(KX_Scene, restart);
561 KX_PYMETHOD_DOC(KX_Scene, replace);
562 KX_PYMETHOD_DOC(KX_Scene, suspend);
563 KX_PYMETHOD_DOC(KX_Scene, resume);
564 KX_PYMETHOD_DOC(KX_Scene, get);
565 KX_PYMETHOD_DOC(KX_Scene, drawObstacleSimulation);
568 static PyObject* pyattr_get_name(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
569 static PyObject* pyattr_get_objects(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
570 static PyObject* pyattr_get_objects_inactive(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
571 static PyObject* pyattr_get_lights(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
572 static PyObject* pyattr_get_cameras(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
573 static PyObject* pyattr_get_active_camera(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
574 static int pyattr_set_active_camera(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
575 static PyObject* pyattr_get_drawing_callback_pre(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
576 static int pyattr_set_drawing_callback_pre(void *selv_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
577 static PyObject* pyattr_get_drawing_callback_post(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
578 static int pyattr_set_drawing_callback_post(void *selv_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
580 virtual PyObject* py_repr(void) { return PyUnicode_FromString(GetName().ReadPtr()); }
582 /* getitem/setitem */
583 static PyMappingMethods Mapping;
584 static PySequenceMethods Sequence;
587 * Run the registered python drawing functions.
589 void RunDrawingCallbacks(PyObject* cb_list);
591 PyObject* GetPreDrawCB() { return m_draw_call_pre; };
592 PyObject* GetPostDrawCB() { return m_draw_call_post; };
596 * Sets the time the scene was suspended
598 void setSuspendedTime(double suspendedtime);
600 * Returns the "curtime" the scene was suspended
602 double getSuspendedTime();
604 * Sets the difference between the local time of the scene (when it
605 * was running and not suspended) and the "curtime"
607 void setSuspendedDelta(double suspendeddelta);
609 * Returns the difference between the local time of the scene (when it
610 * was running and not suspended) and the "curtime"
612 double getSuspendedDelta();
614 * Returns the Blender scene this was made from
616 struct Scene *GetBlenderScene() { return m_blenderScene; }
618 bool MergeScene(KX_Scene *other);
621 //void PrintStats(int verbose_level) {
622 // m_bucketmanager->PrintStats(verbose_level)
627 typedef std::vector<KX_Scene*> KX_SceneList;
629 #endif //__KX_SCENE_H