00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef __CS_ENGINE_H__
00020 #define __CS_ENGINE_H__
00021
00022 #include "csutil/scf.h"
00023 #include "csutil/nobjvec.h"
00024 #include "csutil/csobject.h"
00025 #include "csutil/array.h"
00026 #include "csutil/parray.h"
00027 #include "csutil/stringarray.h"
00028 #include "csutil/refarr.h"
00029 #include "csutil/hashmap.h"
00030 #include "iutil/eventh.h"
00031 #include "iutil/comp.h"
00032 #include "iutil/config.h"
00033 #include "csgeom/math3d.h"
00034 #include "csengine/rview.h"
00035 #include "csengine/meshobj.h"
00036 #include "csengine/region.h"
00037 #include "csengine/sharevar.h"
00038 #include "iengine/engine.h"
00039 #include "iengine/collectn.h"
00040 #include "iengine/campos.h"
00041 #include "iutil/dbghelp.h"
00042 #include "ivideo/graph3d.h"
00043 #include "iengine/renderloop.h"
00044 #include "csengine/renderloop.h"
00045 #include "ivideo/shader/shader.h"
00046 #include "iutil/strset.h"
00047
00048 class csPoly2DPool;
00049
00050 class csRegion;
00051 class csSector;
00052 class csMeshWrapper;
00053 class csTextureList;
00054 class csMaterialList;
00055 class csPolygon3D;
00056 class csCamera;
00057 class csStatLight;
00058 class csDynLight;
00059 class csEngine;
00060 class csLight;
00061 class csLightPatchPool;
00062 class csLightHalo;
00063 class csRenderView;
00064 struct iVFS;
00065 struct iMaterialWrapper;
00066 struct iRegion;
00067 struct iLight;
00068 struct iImageIO;
00069 struct iClipper2D;
00070 struct iReporter;
00071 struct iProgressMeter;
00072 struct iObjectRegistry;
00073 struct iVirtualClock;
00074 struct iCacheManager;
00075 struct iString;
00076 struct iEngineSequenceManager;
00077 struct iConfigFile;
00078
00085 class csLightIt : public iLightIterator
00086 {
00087 private:
00088
00089 csEngine* engine;
00090
00091 iRegion* region;
00092
00093 int sector_idx;
00094
00095 int light_idx;
00096
00097 iLight* current_light;
00098
00099
00100 bool NextSector ();
00101
00103 iLight* FetchNext ();
00104
00105 public:
00107 csLightIt (csEngine*, iRegion* region = 0);
00108
00109 virtual ~csLightIt () { }
00110
00111 SCF_DECLARE_IBASE;
00112
00114 virtual void Reset ();
00115
00117 virtual bool HasNext ();
00118
00120 virtual iLight* Next ();
00121
00123 virtual iSector* GetLastSector ();
00124 };
00125
00129 class csCollectionList : public csRefArrayObject<iCollection>
00130 {
00131 public:
00132 SCF_DECLARE_IBASE;
00133
00135 csCollectionList ();
00136 virtual ~csCollectionList () { }
00138 virtual iCollection* NewCollection (const char* name);
00139
00141 class CollectionList : public iCollectionList
00142 {
00143 public:
00144 SCF_DECLARE_EMBEDDED_IBASE (csCollectionList);
00145 virtual iCollection* NewCollection (const char* name);
00146 virtual int GetCount () const;
00147 virtual iCollection *Get (int n) const;
00148 virtual int Add (iCollection *obj);
00149 virtual bool Remove (iCollection *obj);
00150 virtual bool Remove (int n);
00151 virtual void RemoveAll ();
00152 virtual int Find (iCollection *obj) const;
00153 virtual iCollection *FindByName (const char *Name) const;
00154 } scfiCollectionList;
00155 };
00156
00161 class csCameraPositionList : public csRefArrayObject<iCameraPosition>
00162 {
00163 public:
00164 SCF_DECLARE_IBASE;
00165
00167 csCameraPositionList ();
00168 virtual ~csCameraPositionList () { }
00170 virtual iCameraPosition* NewCameraPosition (const char* name);
00171
00173 class CameraPositionList : public iCameraPositionList
00174 {
00175 SCF_DECLARE_EMBEDDED_IBASE (csCameraPositionList);
00176 virtual iCameraPosition* NewCameraPosition (const char* name);
00177 virtual int GetCount () const;
00178 virtual iCameraPosition *Get (int n) const;
00179 virtual int Add (iCameraPosition *obj);
00180 virtual bool Remove (iCameraPosition *obj);
00181 virtual bool Remove (int n);
00182 virtual void RemoveAll ();
00183 virtual int Find (iCameraPosition *obj) const;
00184 virtual iCameraPosition *FindByName (const char *Name) const;
00185 } scfiCameraPositionList;
00186 };
00187
00191 class csEngineMeshList : public csMeshList
00192 {
00193 public:
00194 virtual ~csEngineMeshList () { RemoveAll (); }
00195 virtual void FreeItem (iMeshWrapper*);
00196 };
00197
00209 struct csEngineConfig : public iConfig
00210 {
00211 SCF_DECLARE_EMBEDDED_IBASE (csEngine);
00212 virtual bool GetOptionDescription (int idx, csOptionDescription *option);
00213 virtual bool SetOption (int id, csVariant* value);
00214 virtual bool GetOption (int id, csVariant* value);
00215 };
00216
00222 class csEngine : public iEngine
00223 {
00224 friend class csRenderLoop;
00225
00226 public:
00235 csRef<iVFS> VFS;
00236
00240 csRef<iEngineSequenceManager> eseqmgr;
00241
00246 csRef<iReporter> Reporter;
00247
00259 csRefArray<iBase> cleanup;
00260
00266 csSectorList sectors;
00267
00272 csCollectionList collections;
00273
00278 csMeshFactoryList mesh_factories;
00279
00287 csEngineMeshList meshes;
00288
00292 csCameraPositionList camera_positions;
00293
00295 static int frame_width, frame_height;
00297 static iObjectRegistry* object_reg;
00299 static csEngine* current_engine;
00301 static iEngine* current_iengine;
00303 static bool use_new_radiosity;
00305 csRef<iGraphics3D> G3D;
00307 csRef<iGraphics2D> G2D;
00309 csRef<iImageIO> ImageLoader;
00311 csRef<iStringSet> Strings;
00313 csRef<iShaderManager> ShaderManager;
00319 csRef<iCacheManager> cache_mgr;
00321 G3D_FOGMETHOD fogmethod;
00323 bool NeedPO2Maps;
00325 int MaxAspectRatio;
00327 csRegionList regions;
00328
00330 csStringArray render_priorities;
00332 csArray<int> render_priority_sortflags;
00334 csArray<bool> render_priority_cameraflags;
00345 long render_priority_sky;
00346 long render_priority_wall;
00347 long render_priority_object;
00348 long render_priority_alpha;
00349
00351 csRef<iRenderLoop> defaultRenderLoop;
00353 csRenderLoopManager* renderLoopManager;
00354
00355 csPtr<iRenderLoop> CreateDefaultRenderLoop ();
00356 void LoadDefaultRenderLoop (const char* fileName);
00357
00359 static int lightcache_mode;
00361 static int lightmap_quality;
00363 static bool do_force_revis;
00365 static bool do_rad_debug;
00367 static int max_lightmap_w;
00368 static int max_lightmap_h;
00369
00370 private:
00372 csTextureList* textures;
00374 csMaterialList* materials;
00376 csDynLight* first_dyn_lights;
00378 csSharedVariableList* shared_variables;
00380 csPDelArray<csLightHalo> halos;
00382 csRef<iMeshObjectType> thing_type;
00384 static int max_process_polygons;
00386 static int cur_process_polygons;
00387
00389 iTextureHandle* render_context;
00390
00392 csHashSet want_to_die;
00393
00395
00396
00398 int default_fastmesh_thresshold;
00400 int fastmesh_thresshold;
00401
00403 bool clear_zbuf;
00404
00406 bool default_clear_zbuf;
00407
00409 bool clear_screen;
00410
00412 bool default_clear_screen;
00413
00415 int default_max_lightmap_w, default_max_lightmap_h;
00416
00418 int default_ambient_red, default_ambient_green, default_ambient_blue;
00419
00428 csTicks nextframe_pending;
00429
00431 csRef<iVirtualClock> virtual_clock;
00432
00433 private:
00437 void StartDraw (iCamera* c, iClipper2D* view, csRenderView& rview);
00438
00442 void ControlMeshes ();
00443
00455 char* SplitRegionName (const char* name, iRegion*& region, bool& global);
00456
00460 void GetNearbyObjectList (iSector* sector,
00461 const csVector3& pos, float radius, csArray<iObject*>& list,
00462 csArray<iSector*>& visited_sectors, bool crossPortals = true);
00463
00467 void GetNearbyMeshList (iSector* sector,
00468 const csVector3& pos, float radius, csArray<iMeshWrapper*>& list,
00469 csArray<iSector*>& visited_sectors, bool crossPortals = true);
00470
00471 public:
00475 iCamera* current_camera;
00476
00480 iClipper2D* top_clipper;
00481
00485 iEngineSequenceManager* GetEngineSequenceManager ();
00486
00487 public:
00493 csEngine (iBase *iParent);
00494
00501 virtual ~csEngine ();
00502
00506 void Report (const char* description, ...) CS_GNUC_PRINTF (2, 3);
00507
00511 void Warn (const char* description, ...) CS_GNUC_PRINTF (2, 3);
00512
00516 void ReportBug (const char* description, ...) CS_GNUC_PRINTF (2, 3);
00517
00525 bool CheckConsistency ();
00526
00534 virtual void PrepareTextures ();
00535
00541 virtual void PrepareMeshes ();
00542
00549 virtual void ShineLights (iRegion* region = 0,
00550 iProgressMeter* meter = 0);
00551
00553 virtual iObject *QueryObject();
00555 inline csObject *QueryCsObject () { return &scfiObject; }
00556
00565 virtual bool Prepare (iProgressMeter* meter = 0);
00566
00578 virtual void ForceRelight (iRegion* region, iProgressMeter* meter);
00579
00590 virtual void ForceRelight (iStatLight* light, iRegion* region);
00591
00601 virtual void RemoveLight (iStatLight* light);
00602
00607 static void SetMaxProcessPolygons (int m) { max_process_polygons = m; }
00608
00612 static int GetMaxProcessPolygons () { return max_process_polygons; }
00613
00618 static bool ProcessPolygon ()
00619 {
00620 if (cur_process_polygons > max_process_polygons) return false;
00621 cur_process_polygons++;
00622 return true;
00623 }
00624
00628 static bool ProcessLastPolygon ()
00629 {
00630 return cur_process_polygons >= max_process_polygons;
00631 }
00632
00637
00638
00644 void InvalidateLightmaps ();
00645
00651 virtual int GetBeginDrawFlags () const
00652 {
00653 int flag = 0;
00654 if (clear_screen) flag |= CSDRAW_CLEARSCREEN;
00655 if (clear_zbuf)
00656 return flag | CSDRAW_CLEARZBUFFER;
00657 else
00658 return flag;
00659 }
00660
00664 csTicks GetLastAnimationTime () const { return nextframe_pending; }
00665
00667 virtual void SetLightingCacheMode (int mode) { lightcache_mode = mode; }
00669 virtual int GetLightingCacheMode () { return lightcache_mode; }
00670
00672 virtual void SetFastMeshThresshold (int th) { fastmesh_thresshold = th; }
00674 virtual int GetFastMeshThresshold () const { return fastmesh_thresshold; }
00675
00677 virtual void SetClearZBuf (bool yesno)
00678 {
00679 clear_zbuf = yesno;
00680 }
00681
00683 virtual bool GetClearZBuf () const { return clear_zbuf; }
00684
00686 virtual bool GetDefaultClearZBuf () const { return default_clear_zbuf; }
00687
00689 virtual void SetClearScreen (bool yesno)
00690 {
00691 clear_screen = yesno;
00692 }
00693
00695 virtual bool GetClearScreen () const { return clear_screen; }
00696
00698 virtual bool GetDefaultClearScreen () const { return default_clear_screen; }
00699
00704 virtual void SetMaxLightmapSize(int w, int h)
00705 { max_lightmap_w = w; max_lightmap_h = h; }
00707 virtual void GetMaxLightmapSize(int& w, int& h)
00708 { w = max_lightmap_w; h = max_lightmap_h; }
00710 virtual void GetDefaultMaxLightmapSize(int& w, int& h)
00711 { w = default_max_lightmap_w; h = default_max_lightmap_h; }
00713 virtual void GetDefaultAmbientLight (csColor &c) const;
00714
00715 virtual bool GetLightmapsRequirePO2 () const { return NeedPO2Maps; }
00716 virtual int GetMaxLightmapAspectRatio () const { return MaxAspectRatio; }
00717
00718 virtual csPtr<iFrustumView> CreateFrustumView ();
00719 virtual csPtr<iObjectWatcher> CreateObjectWatcher ();
00720 virtual void WantToDie (iMeshWrapper* mesh);
00721
00730 virtual void ResetWorldSpecificSettings();
00731
00737 void ReadConfig (iConfigFile*);
00738
00744 void StartEngine ();
00745
00749 csTextureList* GetTextures () const { return textures; }
00750
00754 csMaterialList* GetMaterials () const { return materials; }
00755
00759 csSharedVariableList* GetVariables () const { return shared_variables; }
00760
00764 virtual csPtr<iMaterial> CreateBaseMaterial (iTextureWrapper* txt);
00765 virtual csPtr<iMaterial> CreateBaseMaterial (iTextureWrapper* txt,
00766 int num_layers, iTextureWrapper** wrappers, csTextureLayer* layers);
00767
00768 virtual iSharedVariableList* GetVariableList () const;
00769 virtual iMaterialList* GetMaterialList () const;
00770 virtual iTextureList* GetTextureList () const;
00771
00772 virtual iRegion* CreateRegion (const char* name);
00773 virtual iRegionList* GetRegions ();
00774
00775 virtual csPtr<iMeshWrapper> CreateSectorWallsMesh (iSector* sector,
00776 const char* name);
00777 virtual csPtr<iMeshWrapper> CreateThingMesh (iSector* sector, const char* name);
00778
00782 void AddDynLight (csDynLight* dyn);
00783
00787 void RemoveDynLight (csDynLight* dyn);
00788
00792 virtual iDynLight* GetFirstDynLight () const;
00793
00795 virtual int GetNearbyLights (iSector* sector, const csVector3& pos,
00796 uint32 flags, iLight** lights, int max_num_lights);
00797
00799 virtual int GetNearbyLights (iSector* sector, const csBox3& box,
00800 uint32 flags, iLight** lights, int max_num_lights);
00801
00802 virtual csPtr<iSectorIterator> GetNearbySectors (iSector* sector,
00803 const csVector3& pos, float radius);
00804
00805 virtual csPtr<iObjectIterator> GetNearbyObjects (iSector* sector,
00806 const csVector3& pos, float radius, bool crossPortals = true);
00807 virtual csPtr<iObjectIterator> GetVisibleObjects (iSector* sector,
00808 const csVector3& pos);
00809 virtual csPtr<iObjectIterator> GetVisibleObjects (iSector* sector,
00810 const csFrustum& frustum);
00811
00812 virtual csPtr<iMeshWrapperIterator> GetNearbyMeshes (iSector* sector,
00813 const csVector3& pos, float radius, bool crossPortals = true);
00814 virtual csPtr<iMeshWrapperIterator> GetVisibleMeshes (iSector* sector,
00815 const csVector3& pos);
00816 virtual csPtr<iMeshWrapperIterator> GetVisibleMeshes (iSector* sector,
00817 const csFrustum& frustum);
00818
00819 virtual bool RemoveObject (iBase* object);
00820
00824 void AddHalo (csLight* Light);
00825
00829 void RemoveHalo (csLight* Light);
00830
00838 virtual void Draw (iCamera* c, iClipper2D* clipper);
00839
00843 virtual csPtr<iLightIterator> GetLightIterator (iRegion* region = 0)
00844 {
00845 csLightIt* it;
00846 it = new csLightIt (this, region);
00847 return csPtr<iLightIterator> (it);
00848 }
00849
00851 virtual void RegisterRenderPriority (const char* name, long priority,
00852 int rendsort = CS_RENDPRI_NONE, bool do_camera = false);
00854 virtual long GetRenderPriority (const char* name) const;
00856 virtual void SetRenderPriorityCamera (long priority, bool do_camera);
00858 virtual bool GetRenderPriorityCamera (const char* name) const;
00860 virtual bool GetRenderPriorityCamera (long priority) const;
00862 virtual int GetRenderPrioritySorting (const char* name) const;
00864 virtual int GetRenderPrioritySorting (long priority) const;
00866 virtual long GetSkyRenderPriority () const { return render_priority_sky; }
00868 virtual long GetWallRenderPriority () const { return render_priority_wall; }
00870 virtual long GetObjectRenderPriority () const { return render_priority_object; }
00872 virtual long GetAlphaRenderPriority () const { return render_priority_alpha; }
00874 virtual void ClearRenderPriorities ();
00876 virtual int GetRenderPriorityCount () const;
00878 virtual const char* GetRenderPriorityName (long priority) const;
00879
00880 iMeshObjectType* GetThingType ();
00881
00882 SCF_DECLARE_IBASE;
00883
00884
00885
00891 virtual bool Initialize (iObjectRegistry* object_reg);
00892
00894 virtual bool HandleEvent (iEvent &Event);
00895
00897 struct eiComponent : public iComponent
00898 {
00899 SCF_DECLARE_EMBEDDED_IBASE(csEngine);
00900 virtual bool Initialize (iObjectRegistry* p)
00901 { return scfParent->Initialize(p); }
00902 } scfiComponent;
00903
00905 struct EventHandler : public iEventHandler
00906 {
00907 private:
00908 csEngine* parent;
00909 public:
00910 SCF_DECLARE_IBASE;
00911 EventHandler (csEngine* parent)
00912 {
00913 SCF_CONSTRUCT_IBASE (0);
00914 EventHandler::parent = parent;
00915 }
00916 virtual bool HandleEvent (iEvent& e) { return parent->HandleEvent(e); }
00917 } * scfiEventHandler;
00918
00919
00920
00925 virtual int GetTextureFormat () const;
00926
00928 virtual void DeleteAll ();
00929
00931 virtual iTextureWrapper* CreateTexture (const char *iName,
00932 const char *iFileName, csColor *iTransp, int iFlags);
00933 virtual iTextureWrapper* CreateBlackTexture (const char *name,
00934 int w, int h, csColor *iTransp, int iFlags);
00936 virtual iMaterialWrapper* CreateMaterial (const char *iName,
00937 iTextureWrapper* texture);
00938
00940 virtual iSector *CreateSector (const char *iName);
00941
00943 virtual iSectorList *GetSectors ()
00944 { return §ors; }
00946 virtual iMeshFactoryList *GetMeshFactories ()
00947 { return &mesh_factories; }
00949 virtual iMeshList *GetMeshes ()
00950 { return &meshes; }
00952 virtual iCollectionList *GetCollections ()
00953 { return &collections.scfiCollectionList; }
00955 virtual iCameraPositionList *GetCameraPositions ()
00956 { return &camera_positions.scfiCameraPositionList; }
00957
00959 virtual csPtr<iCamera> CreateCamera ();
00960
00962 virtual csPtr<iStatLight> CreateLight (const char* name,
00963 const csVector3& pos, float radius,
00964 const csColor& color, bool pseudoDyn);
00966 virtual iStatLight* FindLightID (const char* light_id) const;
00968 virtual iStatLight* FindLight (const char *Name, bool RegionOnly = false)
00969 const;
00971 virtual csPtr<iDynLight> CreateDynLight (const csVector3& pos, float radius,
00972 const csColor& color);
00974 virtual void RemoveDynLight (iDynLight*);
00975
00977 virtual csPtr<iMeshFactoryWrapper> CreateMeshFactory (const char* classId,
00978 const char* name);
00980 virtual csPtr<iMeshFactoryWrapper> CreateMeshFactory (iMeshObjectFactory *,
00981 const char* name);
00983 virtual csPtr<iMeshFactoryWrapper> CreateMeshFactory (const char* name);
00984 virtual csPtr<iLoaderContext> CreateLoaderContext (iRegion* region,
00985 bool curRegOnly);
00987 virtual csPtr<iMeshFactoryWrapper> LoadMeshFactory (
00988 const char* name, const char* loaderClassId,
00989 iDataBuffer* input);
00990
00992 virtual csPtr<iMeshWrapper> CreateMeshWrapper (iMeshFactoryWrapper* factory,
00993 const char* name, iSector* sector = 0,
00994 const csVector3& pos = csVector3(0, 0, 0));
00996 virtual csPtr<iMeshWrapper> CreateMeshWrapper (iMeshObject*,
00997 const char* name, iSector* sector = 0,
00998 const csVector3& pos = csVector3(0, 0, 0));
01000 virtual csPtr<iMeshWrapper> CreateMeshWrapper (const char* classid,
01001 const char* name, iSector* sector = 0,
01002 const csVector3& pos = csVector3(0, 0, 0));
01004 virtual csPtr<iMeshWrapper> CreateMeshWrapper (const char* name);
01006 virtual csPtr<iMeshWrapper> LoadMeshWrapper (
01007 const char* name, const char* loaderClassId,
01008 iDataBuffer* input, iSector* sector, const csVector3& pos);
01009
01010 virtual iClipper2D* GetTopLevelClipper () const;
01011
01013 virtual void SetAmbientLight (const csColor &c);
01015 virtual void GetAmbientLight (csColor &c) const;
01016
01017 virtual iMaterialWrapper* FindMaterial (const char* name,
01018 iRegion* region = 0);
01019 virtual iTextureWrapper* FindTexture (const char* name,
01020 iRegion* region = 0);
01021 virtual iSector* FindSector (const char* name,
01022 iRegion* region = 0);
01023 virtual iMeshWrapper* FindMeshObject (const char* name,
01024 iRegion* region = 0);
01025 virtual iMeshFactoryWrapper* FindMeshFactory (const char* name,
01026 iRegion* region = 0);
01027 virtual iCameraPosition* FindCameraPosition (const char* name,
01028 iRegion* region = 0);
01029 virtual iCollection* FindCollection (const char* name,
01030 iRegion* region = 0);
01031
01032 bool DebugCommand (const char* cmd);
01033
01034
01035
01036 virtual void SetCacheManager (iCacheManager* cache_mgr);
01037 virtual iCacheManager* GetCacheManager ();
01038
01039
01040
01041 csEngineConfig scfiConfig;
01042
01043
01044
01046 virtual void SetContext (iTextureHandle* txt);
01048 virtual iTextureHandle *GetContext () const;
01049
01050
01051
01052
01053
01054 virtual iRenderLoopManager* GetRenderLoopManager ();
01055 virtual iRenderLoop* GetCurrentDefaultRenderloop ();
01056 virtual bool SetCurrentDefaultRenderloop (iRenderLoop* loop);
01057
01058 private:
01060 void Resize ();
01062 bool resize;
01063
01067 class iObjectInterface : public csObject
01068 {
01069 SCF_DECLARE_EMBEDDED_IBASE (csEngine);
01070 } scfiObject;
01071
01072 struct DebugHelper : public iDebugHelper
01073 {
01074 SCF_DECLARE_EMBEDDED_IBASE (csEngine);
01075 virtual int GetSupportedTests () const
01076 {
01077 return 0;
01078 }
01079 virtual csPtr<iString> UnitTest () { return 0; }
01080 virtual csPtr<iString> StateTest () { return 0; }
01081 virtual csTicks Benchmark (int) { return 0; }
01082 virtual csPtr<iString> Dump () { return 0; }
01083 virtual void Dump (iGraphics3D*) { }
01084 virtual bool DebugCommand (const char* cmd)
01085 {
01086 return scfParent->DebugCommand (cmd);
01087 }
01088 } scfiDebugHelper;
01089 };
01090
01091 #endif // __CS_ENGINE_H__