Blender  V2.59
KX_Scene.h
Go to the documentation of this file.
00001 /*
00002  * $Id: KX_Scene.h 36523 2011-05-06 20:18:42Z blendix $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00021  * All rights reserved.
00022  *
00023  * The Original Code is: all of this file.
00024  *
00025  * Contributor(s): none yet.
00026  *
00027  * ***** END GPL LICENSE BLOCK *****
00028  */
00029 
00034 #ifndef __KX_SCENE_H
00035 #define __KX_SCENE_H
00036 
00037 
00038 #include "KX_PhysicsEngineEnums.h"
00039 
00040 #include <vector>
00041 #include <set>
00042 #include <list>
00043 
00044 #include "CTR_Map.h"
00045 #include "CTR_HashedPtr.h"
00046 #include "SG_IObject.h"
00047 #include "SCA_IScene.h"
00048 #include "MT_Transform.h"
00049 
00050 #include "RAS_FramingManager.h"
00051 #include "RAS_Rect.h"
00052 
00053 
00054 #include "PyObjectPlus.h"
00055 #include "RAS_2DFilterManager.h"
00056 
00060 struct SM_MaterialProps;
00061 struct SM_ShapeProps;
00062 struct Scene;
00063 
00064 class CTR_HashedPtr;
00065 class CListValue;
00066 class CValue;
00067 class SCA_LogicManager;
00068 class SCA_KeyboardManager;
00069 class SCA_TimeEventManager;
00070 class SCA_MouseManager;
00071 class SCA_ISystem;
00072 class SCA_IInputDevice;
00073 class NG_NetworkDeviceInterface;
00074 class NG_NetworkScene;
00075 class SG_IObject;
00076 class SG_Node;
00077 class SG_Tree;
00078 class KX_WorldInfo;
00079 class KX_Camera;
00080 class KX_GameObject;
00081 class KX_LightObject;
00082 class RAS_BucketManager;
00083 class RAS_MaterialBucket;
00084 class RAS_IPolyMaterial;
00085 class RAS_IRasterizer;
00086 class RAS_IRenderTools;
00087 class SCA_JoystickManager;
00088 class btCollisionShape;
00089 class KX_BlenderSceneConverter;
00090 struct KX_ClientObjectInfo;
00091 
00092 #ifdef WITH_CXX_GUARDEDALLOC
00093 #include "MEM_guardedalloc.h"
00094 #endif
00095 
00096 /* for ID freeing */
00097 #define IS_TAGGED(_id) ((_id) && (((ID *)_id)->flag & LIB_DOIT))
00098 
00103 class KX_Scene : public PyObjectPlus, public SCA_IScene
00104 {
00105         Py_Header;
00106 
00107 #ifdef WITH_PYTHON
00108         PyObject*       m_attr_dict;
00109         PyObject*       m_draw_call_pre;
00110         PyObject*       m_draw_call_post;
00111 #endif
00112 
00113         struct CullingInfo {
00114                 int m_layer;
00115                 CullingInfo(int layer) : m_layer(layer) {}
00116         };
00117 
00118 protected:
00119         RAS_BucketManager*      m_bucketmanager;
00120         CListValue*                     m_tempObjectList;
00121 
00127         CListValue*     m_euthanasyobjects;
00128 
00129         CListValue*                     m_objectlist;
00130         CListValue*                     m_parentlist; // all 'root' parents
00131         CListValue*                     m_lightlist;
00132         CListValue*                     m_inactivelist; // all objects that are not in the active layer
00133         
00134         SG_QList                        m_sghead;               // list of nodes that needs scenegraph update
00135                                                                                 // the Dlist is not object that must be updated
00136                                                                                 // the Qlist is for objects that needs to be rescheduled
00137                                                                                 // for updates after udpate is over (slow parent, bone parent)
00138 
00139 
00143         list<class KX_Camera*>       m_cameras;
00144 
00148         list<class KX_FontObject*>       m_fonts;
00149 
00150 
00154         SCA_LogicManager*               m_logicmgr;
00155         SCA_KeyboardManager*    m_keyboardmgr;
00156         SCA_MouseManager*               m_mousemgr;
00157         SCA_TimeEventManager*   m_timemgr;
00158 
00159         // Scene converter where many scene entities are registered
00160         // Used to deregister objects that are deleted
00161         class KX_BlenderSceneConverter*         m_sceneConverter;
00165         //e_PhysicsEngine m_physicsEngine; //who needs this ?
00166         class PHY_IPhysicsEnvironment*          m_physicsEnvironment;
00167 
00171         bool m_isclearingZbuffer;
00172 
00176         STR_String      m_sceneName;
00177         
00181         KX_WorldInfo* m_worldinfo;
00182 
00190         NG_NetworkDeviceInterface*      m_networkDeviceInterface;
00191         NG_NetworkScene* m_networkScene;
00192 
00198         SG_Node* m_rootnode;
00199 
00203         KX_Camera* m_active_camera;
00204 
00210         CTR_Map	<CTR_HashedPtr, void*> m_map_gameobject_to_replica;
00211 
00218         std::vector<KX_GameObject*>     m_logicHierarchicalGameObjects;
00219         
00228         std::set<CValue*>       m_groupGameObjects;
00229         
00236         SCA_ISystem* m_kxsystem;
00237 
00241         int     m_ueberExecutionPriority;
00242 
00247         bool m_suspend;
00248 
00252         float m_activity_box_radius;
00253 
00257         bool m_activity_culling;
00258         
00262         bool m_dbvt_culling;
00263         
00267         int m_dbvt_occlusion_res;
00268 
00273         RAS_FrameSettings m_frame_settings;
00274 
00279         RAS_Rect m_viewport;
00280         
00284         void MarkVisible(SG_Tree *node, RAS_IRasterizer* rasty, KX_Camera*cam,int layer=0);
00285         void MarkSubTreeVisible(SG_Tree *node, RAS_IRasterizer* rasty, bool visible, KX_Camera*cam,int layer=0);
00286         void MarkVisible(RAS_IRasterizer* rasty, KX_GameObject* gameobj, KX_Camera*cam, int layer=0);
00287         static void PhysicsCullingCallback(KX_ClientObjectInfo* objectInfo, void* cullingInfo);
00288 
00289         double                          m_suspendedtime;
00290         double                          m_suspendeddelta;
00291 
00292         struct Scene* m_blenderScene;
00293 
00294         RAS_2DFilterManager m_filtermanager;
00295 public: 
00296         KX_Scene(class SCA_IInputDevice* keyboarddevice,
00297                 class SCA_IInputDevice* mousedevice,
00298                 class NG_NetworkDeviceInterface* ndi,
00299                 const STR_String& scenename,
00300                 struct Scene* scene,
00301                 class RAS_ICanvas* canvas);
00302 
00303         virtual 
00304         ~KX_Scene();
00305 
00306         RAS_BucketManager* GetBucketManager();
00307         RAS_MaterialBucket*     FindBucket(RAS_IPolyMaterial* polymat, bool &bucketCreated);
00308         void RenderBuckets(const MT_Transform& cameratransform,
00309                                                 RAS_IRasterizer* rasty,
00310                                                 RAS_IRenderTools* rendertools);
00311 
00315         static bool KX_ScenegraphUpdateFunc(SG_IObject* node,void* gameobj,void* scene);
00316         static bool KX_ScenegraphRescheduleFunc(SG_IObject* node,void* gameobj,void* scene);
00317         void UpdateParents(double curtime);
00318         void DupliGroupRecurse(CValue* gameobj, int level);
00319         bool IsObjectInGroup(CValue* gameobj)
00320         { 
00321                 return (m_groupGameObjects.empty() || 
00322                                 m_groupGameObjects.find(gameobj) != m_groupGameObjects.end());
00323         }
00324         SCA_IObject* AddReplicaObject(CValue* gameobj,
00325                                                                   CValue* locationobj,
00326                                                                   int lifespan=0);
00327         KX_GameObject* AddNodeReplicaObject(SG_IObject* node,
00328                                                                                 CValue* gameobj);
00329         void RemoveNodeDestructObject(SG_IObject* node,
00330                                                                   CValue* gameobj);
00331         void RemoveObject(CValue* gameobj);
00332         void DelayedRemoveObject(CValue* gameobj);
00333         
00334         int NewRemoveObject(CValue* gameobj);
00335         void ReplaceMesh(CValue* gameobj,
00336                                          void* meshob, bool use_gfx, bool use_phys);
00341         void LogicBeginFrame(double curtime);
00342         void LogicUpdateFrame(double curtime, bool frame);
00343 
00344                 void                                            
00345         LogicEndFrame(
00346         );
00347 
00348                 CListValue*                             
00349         GetTempObjectList(
00350         );
00351 
00352                 CListValue*
00353         GetObjectList(
00354         );
00355 
00356                 CListValue*                             
00357         GetInactiveList(
00358         );
00359 
00360                 CListValue*                             
00361         GetRootParentList(
00362         );
00363 
00364                 CListValue*                             
00365         GetLightList(
00366         );
00367 
00368                 SCA_LogicManager*               
00369         GetLogicManager(
00370         );
00371 
00372                 SCA_TimeEventManager*   
00373         GetTimeEventManager(
00374         );
00375 
00378                 list<class KX_FontObject*>*
00379         GetFonts(
00380         );
00381 
00383                 KX_FontObject*              
00384         FindFont(
00385                 KX_FontObject*
00386         );
00387 
00389                 void                    
00390         AddFont(
00391                 KX_FontObject*
00392         );
00393 
00394 
00397                 list<class KX_Camera*>*
00398         GetCameras(
00399         );
00400  
00401 
00403                 KX_Camera*              
00404         FindCamera(
00405                 KX_Camera*
00406         );
00407 
00409                 KX_Camera*              
00410         FindCamera(
00411                 STR_String&
00412         );
00413 
00415                 void                    
00416         AddCamera(
00417                 KX_Camera*
00418         );
00419 
00421                 KX_Camera*                              
00422         GetActiveCamera(
00423         );
00424 
00430                 void                                    
00431         SetActiveCamera(
00432                 class KX_Camera*
00433         );
00434 
00439                 void
00440         SetCameraOnTop(
00441                 class KX_Camera*
00442         );
00443 
00448                 void                                    
00449         SetCanvasDesignWidth(
00450                 unsigned int width
00451         );
00456                 void                                    
00457         SetCanvasDesignHeight(
00458                 unsigned int height
00459         );
00464                 unsigned int                    
00465         GetCanvasDesignWidth(
00466                 void
00467         ) const;
00468 
00473                 unsigned int                    
00474         GetCanvasDesignHeight(
00475                 void
00476         ) const;
00477 
00482                 void
00483         SetFramingType(
00484                 RAS_FrameSettings & frame_settings
00485         );
00486 
00494         const
00495                 RAS_FrameSettings &
00496         GetFramingType(
00497         ) const;
00498 
00503         void SetSceneViewport(const RAS_Rect &viewport);
00504 
00510         const RAS_Rect& GetSceneViewport() const;
00511         
00515         void SetNetworkDeviceInterface(NG_NetworkDeviceInterface* newInterface);
00516         void SetNetworkScene(NG_NetworkScene *newScene);
00517         void SetWorldInfo(class KX_WorldInfo* wi);
00518         KX_WorldInfo* GetWorldInfo();
00519         void CalculateVisibleMeshes(RAS_IRasterizer* rasty, KX_Camera *cam, int layer=0);
00520         void UpdateMeshTransformations();
00521         KX_Camera* GetpCamera();
00522         NG_NetworkDeviceInterface* GetNetworkDeviceInterface();
00523         NG_NetworkScene* GetNetworkScene();
00524         KX_BlenderSceneConverter *GetSceneConverter() { return m_sceneConverter; }
00525 
00530         void ReplicateLogic(class KX_GameObject* newobj);
00531         static SG_Callbacks     m_callbacks;
00532 
00533         const STR_String& GetName();
00534         
00535         // Suspend the entire scene.
00536         void Suspend();
00537 
00538         // Resume a suspended scene.
00539         void Resume();
00540         
00541         // Update the activity box settings for objects in this scene, if needed.
00542         void UpdateObjectActivity(void);
00543 
00544         // Enable/disable activity culling.
00545         void SetActivityCulling(bool b);
00546 
00547         // Set the radius of the activity culling box.
00548         void SetActivityCullingRadius(float f);
00549         bool IsSuspended();
00550         bool IsClearingZBuffer();
00551         void EnableZBufferClearing(bool isclearingZbuffer);
00552         // use of DBVT tree for camera culling
00553         void SetDbvtCulling(bool b) { m_dbvt_culling = b; };
00554         bool GetDbvtCulling() { return m_dbvt_culling; };
00555         void SetDbvtOcclusionRes(int i) { m_dbvt_occlusion_res = i; };
00556         int GetDbvtOcclusionRes() { return m_dbvt_occlusion_res; };
00557         
00558         void SetSceneConverter(class KX_BlenderSceneConverter* sceneConverter);
00559 
00560         class PHY_IPhysicsEnvironment*          GetPhysicsEnvironment()
00561         {
00562                 return m_physicsEnvironment;
00563         }
00564 
00565         void SetPhysicsEnvironment(class PHY_IPhysicsEnvironment*       physEnv);
00566 
00567         void    SetGravity(const MT_Vector3& gravity);
00568         
00572         void SetNodeTree(SG_Tree* root);
00573 
00577         void Update2DFilter(vector<STR_String>& propNames, void* gameObj, RAS_2DFilterManager::RAS_2DFILTER_MODE filtermode, int pass, STR_String& text);
00578         void Render2DFilters(RAS_ICanvas* canvas);
00579 
00580 #ifdef WITH_PYTHON
00581         /* --------------------------------------------------------------------- */
00582         /* Python interface ---------------------------------------------------- */
00583         /* --------------------------------------------------------------------- */
00584 
00585         KX_PYMETHOD_DOC(KX_Scene, addObject);
00586         KX_PYMETHOD_DOC(KX_Scene, end);
00587         KX_PYMETHOD_DOC(KX_Scene, restart);
00588         KX_PYMETHOD_DOC(KX_Scene, replace);
00589         KX_PYMETHOD_DOC(KX_Scene, suspend);
00590         KX_PYMETHOD_DOC(KX_Scene, resume);
00591         KX_PYMETHOD_DOC(KX_Scene, get);
00592 
00593         /* attributes */
00594         static PyObject*        pyattr_get_name(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
00595         static PyObject*        pyattr_get_objects(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
00596         static PyObject*        pyattr_get_objects_inactive(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
00597         static PyObject*        pyattr_get_lights(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
00598         static PyObject*        pyattr_get_cameras(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
00599         static PyObject*        pyattr_get_active_camera(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
00600         static int                      pyattr_set_active_camera(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
00601         static PyObject*        pyattr_get_drawing_callback_pre(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
00602         static int                      pyattr_set_drawing_callback_pre(void *selv_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
00603         static PyObject*        pyattr_get_drawing_callback_post(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
00604         static int                      pyattr_set_drawing_callback_post(void *selv_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
00605 
00606         virtual PyObject* py_repr(void) { return PyUnicode_FromString(GetName().ReadPtr()); }
00607         
00608         /* getitem/setitem */
00609         static PyMappingMethods Mapping;
00610         static PySequenceMethods        Sequence;
00611 
00615         void RunDrawingCallbacks(PyObject* cb_list);
00616         
00617         PyObject* GetPreDrawCB() { return m_draw_call_pre; };
00618         PyObject* GetPostDrawCB() { return m_draw_call_post; };
00619 #endif
00620 
00624         void setSuspendedTime(double suspendedtime);
00628         double getSuspendedTime();
00633         void setSuspendedDelta(double suspendeddelta);
00638         double getSuspendedDelta();
00642         struct Scene *GetBlenderScene() { return m_blenderScene; }
00643 
00644         bool MergeScene(KX_Scene *other);
00645 
00646 
00647         //void PrintStats(int verbose_level) {
00648         //      m_bucketmanager->PrintStats(verbose_level)
00649         //}
00650 };
00651 
00652 typedef std::vector<KX_Scene*> KX_SceneList;
00653 
00654 #endif //__KX_SCENE_H
00655