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/garray.h"
00026 #include "iutil/eventh.h"
00027 #include "iutil/comp.h"
00028 #include "iutil/config.h"
00029 #include "csgeom/math3d.h"
00030 #include "csengine/arrays.h"
00031 #include "csengine/rview.h"
00032 #include "csengine/thing.h"
00033 #include "csengine/meshobj.h"
00034 #include "csengine/region.h"
00035 #include "iengine/engine.h"
00036 #include "iengine/collectn.h"
00037 #include "iengine/campos.h"
00038 #include "ivideo/graph3d.h"
00039
00040 class csRegion;
00041 class csRadiosity;
00042 class csSector;
00043 class csMeshWrapper;
00044 class csTextureList;
00045 class csMaterialList;
00046 class csPolygon3D;
00047 class csCamera;
00048 class csStatLight;
00049 class csDynLight;
00050 class csCBufferCube;
00051 class csEngine;
00052 class csLight;
00053 class csCBuffer;
00054 class csPoly2DPool;
00055 class csLightPatchPool;
00056 class csLightHalo;
00057 class csRenderView;
00058 struct iVFS;
00059 struct iMaterialWrapper;
00060 struct iRegion;
00061 struct iLight;
00062 struct iImageIO;
00063 struct iClipper2D;
00064 struct iReporter;
00065 struct iProgressMeter;
00066 struct iObjectRegistry;
00067 struct iVirtualClock;
00068 struct iCacheManager;
00069
00070 SCF_DECLARE_FAST_INTERFACE (iEngine)
00071 SCF_DECLARE_FAST_INTERFACE (iSector)
00072 SCF_DECLARE_FAST_INTERFACE (iMeshWrapper)
00073 SCF_DECLARE_FAST_INTERFACE (iCollection)
00074 SCF_DECLARE_FAST_INTERFACE (iMeshFactoryWrapper)
00075 SCF_DECLARE_FAST_INTERFACE (iCurveTemplate)
00076 SCF_DECLARE_FAST_INTERFACE (iMaterialWrapper)
00077 SCF_DECLARE_FAST_INTERFACE (iTextureWrapper)
00078 SCF_DECLARE_FAST_INTERFACE (iCameraPosition)
00079 SCF_DECLARE_FAST_INTERFACE (iPolyTxtPlane)
00080 SCF_DECLARE_FAST_INTERFACE (iStatLight)
00081 SCF_DECLARE_FAST_INTERFACE (iDynLight)
00082 SCF_DECLARE_FAST_INTERFACE (iLightingInfo)
00083 SCF_DECLARE_FAST_INTERFACE (iLight)
00084 SCF_DECLARE_FAST_INTERFACE (iMeshObject)
00085 SCF_DECLARE_FAST_INTERFACE (iMaterial)
00086 SCF_DECLARE_FAST_INTERFACE (iCrossHalo)
00087 SCF_DECLARE_FAST_INTERFACE (iNovaHalo)
00088 SCF_DECLARE_FAST_INTERFACE (iFlareHalo)
00089 SCF_DECLARE_FAST_INTERFACE (iThingState)
00090 SCF_DECLARE_FAST_INTERFACE (iShadowCaster)
00091 SCF_DECLARE_FAST_INTERFACE (iPolygon3D)
00092 SCF_DECLARE_FAST_INTERFACE (iMeshObjectFactory)
00093 SCF_DECLARE_FAST_INTERFACE (iReferencedObject)
00094 SCF_DECLARE_FAST_INTERFACE (iFile)
00095 SCF_DECLARE_FAST_INTERFACE (iVisibilityObject)
00096 SCF_DECLARE_FAST_INTERFACE (iVisibilityCuller)
00097
00098 SCF_DECLARE_FAST_INTERFACE (csRadPoly)
00099 SCF_DECLARE_FAST_INTERFACE (csRadCurve)
00100
00107 class csLightIt
00108 {
00109 private:
00110
00111 csEngine* engine;
00112
00113 iRegion* region;
00114
00115 int sector_idx;
00116
00117 int light_idx;
00118
00119
00120 bool NextSector ();
00121
00122 public:
00124 csLightIt (csEngine*, iRegion* region = NULL);
00125
00127 void Restart ();
00128
00130 csLight* Fetch ();
00131
00133 csSector* GetLastSector ();
00134 };
00135
00136 CS_DECLARE_OBJECT_VECTOR (csCollectionListHelper, iCollection);
00137
00141 class csCollectionList : public csCollectionListHelper
00142 {
00143 public:
00144 SCF_DECLARE_IBASE;
00145
00147 csCollectionList ();
00149 virtual iCollection* NewCollection (const char* name);
00150
00151 class CollectionList : public iCollectionList
00152 {
00153 public:
00154 SCF_DECLARE_EMBEDDED_IBASE (csCollectionList);
00155 virtual iCollection* NewCollection (const char* name);
00156 virtual int GetCount () const;
00157 virtual iCollection *Get (int n) const;
00158 virtual int Add (iCollection *obj);
00159 virtual bool Remove (iCollection *obj);
00160 virtual bool Remove (int n);
00161 virtual void RemoveAll ();
00162 virtual int Find (iCollection *obj) const;
00163 virtual iCollection *FindByName (const char *Name) const;
00164 } scfiCollectionList;
00165 };
00166
00167 CS_DECLARE_OBJECT_VECTOR (csCameraPositionListHelper, iCameraPosition);
00168
00173 class csCameraPositionList : public csCameraPositionListHelper
00174 {
00175 public:
00176 SCF_DECLARE_IBASE;
00177
00179 csCameraPositionList ();
00181 virtual iCameraPosition* NewCameraPosition (const char* name);
00182
00183 class CameraPositionList : public iCameraPositionList
00184 {
00185 SCF_DECLARE_EMBEDDED_IBASE (csCameraPositionList);
00186 virtual iCameraPosition* NewCameraPosition (const char* name);
00187 virtual int GetCount () const;
00188 virtual iCameraPosition *Get (int n) const;
00189 virtual int Add (iCameraPosition *obj);
00190 virtual bool Remove (iCameraPosition *obj);
00191 virtual bool Remove (int n);
00192 virtual void RemoveAll ();
00193 virtual int Find (iCameraPosition *obj) const;
00194 virtual iCameraPosition *FindByName (const char *Name) const;
00195 } scfiCameraPositionList;
00196 };
00197
00201 class csEngineMeshList : public csMeshList
00202 {
00203 public:
00204 virtual ~csEngineMeshList ();
00205 virtual bool FreeItem (csSome Item);
00206 };
00207
00219 struct csEngineConfig : public iConfig
00220 {
00221 SCF_DECLARE_EMBEDDED_IBASE (csEngine);
00222 virtual bool GetOptionDescription (int idx, csOptionDescription *option);
00223 virtual bool SetOption (int id, csVariant* value);
00224 virtual bool GetOption (int id, csVariant* value);
00225 };
00226
00232 class csEngine : public iEngine
00233 {
00234 public:
00243 iVFS *VFS;
00244
00249 iReporter* Reporter;
00250
00262 csObjVector cleanup;
00263
00269 csSectorList sectors;
00270
00275 csCollectionList collections;
00276
00281 csMeshFactoryList mesh_factories;
00282
00290 csEngineMeshList meshes;
00291
00295 csCameraPositionList camera_positions;
00296
00298 static int frame_width, frame_height;
00300 static iObjectRegistry* object_reg;
00302 static csEngine* current_engine;
00304 static iEngine* current_iengine;
00306 static bool use_new_radiosity;
00308 csPoly2DPool* render_pol2d_pool;
00310 csLightPatchPool* lightpatch_pool;
00312 iGraphics3D* G3D;
00314 iGraphics2D* G2D;
00316 iImageIO* ImageLoader;
00318 G3D_FOGMETHOD fogmethod;
00320 bool NeedPO2Maps;
00322 int MaxAspectRatio;
00324 iRegion* region;
00326 csRegionList regions;
00327
00329 csVector render_priorities;
00331 CS_DECLARE_GROWING_ARRAY (render_priority_sortflags, int);
00342 long render_priority_sky;
00343 long render_priority_wall;
00344 long render_priority_object;
00345 long render_priority_alpha;
00346
00348 static int lightcache_mode;
00350 static int lightmap_quality;
00352 static bool do_force_revis;
00354 static bool do_rad_debug;
00356 static int max_lightmap_w;
00357 static int max_lightmap_h;
00358
00359 private:
00361 csTextureList* textures;
00363 csMaterialList* materials;
00365 csDynLight* first_dyn_lights;
00367 csHaloArray halos;
00369 static int max_process_polygons;
00371 static int cur_process_polygons;
00372
00374 int engine_mode;
00375
00377 csRadiosity* rad_debug;
00378
00380 csCBuffer* c_buffer;
00381
00383 csCBufferCube* cbufcube;
00384
00386 bool use_pvs;
00387
00392 bool use_pvs_only;
00393
00400 bool freeze_pvs;
00402 csVector3 freeze_pvs_pos;
00403
00405 bool clear_zbuf;
00406
00408 bool default_clear_zbuf;
00409
00411 int default_lightmap_cell_size;
00412
00414 int default_max_lightmap_w, default_max_lightmap_h;
00415
00424 csTicks nextframe_pending;
00425
00427 iVirtualClock* virtual_clock;
00428
00429 private:
00433 void ResolveEngineMode ();
00434
00438 void StartDraw (iCamera* c, iClipper2D* view, csRenderView& rview);
00439
00443 void ControlMeshes ();
00444
00456 char* SplitRegionName (const char* name, iRegion*& region, bool& global);
00457
00463 iCacheManager* cache_mgr;
00464
00465 public:
00469 iCamera* current_camera;
00470
00474 iClipper2D* top_clipper;
00475
00476 public:
00482 csEngine (iBase *iParent);
00483
00490 virtual ~csEngine ();
00491
00495 void Report (const char* description, ...) CS_GNUC_PRINTF (2, 3);
00496
00500 void Warn (const char* description, ...) CS_GNUC_PRINTF (2, 3);
00501
00505 void ReportBug (const char* description, ...) CS_GNUC_PRINTF (2, 3);
00506
00514 bool CheckConsistency ();
00515
00523 virtual void PrepareTextures ();
00524
00530 virtual void PrepareMeshes ();
00531
00538 virtual void ShineLights (iRegion* region = NULL,
00539 iProgressMeter* meter = NULL);
00540
00542 virtual iObject *QueryObject();
00544 inline csObject *QueryCsObject () { return &scfiObject; }
00545
00554 virtual bool Prepare (iProgressMeter* meter = NULL);
00555
00560 static void SetMaxProcessPolygons (int m) { max_process_polygons = m; }
00561
00565 static int GetMaxProcessPolygons () { return max_process_polygons; }
00566
00571 static bool ProcessPolygon ()
00572 {
00573 if (cur_process_polygons > max_process_polygons) return false;
00574 cur_process_polygons++;
00575 return true;
00576 }
00577
00581 static bool ProcessLastPolygon ()
00582 {
00583 return cur_process_polygons >= max_process_polygons;
00584 }
00585
00590 csRadiosity* GetRadiosity () const { return rad_debug; }
00591
00597 void InvalidateLightmaps ();
00598
00606 virtual void SetEngineMode (int mode)
00607 {
00608 engine_mode = mode;
00609 }
00610
00616 virtual int GetEngineMode () const { return engine_mode; }
00617
00623 virtual int GetBeginDrawFlags () const
00624 {
00625 if (clear_zbuf || engine_mode == CS_ENGINE_ZBUFFER)
00626 return CSDRAW_CLEARZBUFFER;
00627 else
00628 return 0;
00629 }
00630
00634 csTicks GetLastAnimationTime () const { return nextframe_pending; }
00635
00639 void InitCuller ();
00640
00644 csCBuffer* GetCBuffer () const { return c_buffer; }
00645
00649 csCBufferCube* GetCBufCube () const { return cbufcube; }
00650
00654 void EnablePVS () { use_pvs = true; use_pvs_only = false; }
00655
00659 void DisablePVS () { use_pvs = false; }
00660
00664 virtual bool IsPVS () const { return use_pvs; }
00665
00670 void EnablePVSOnly () { use_pvs_only = true; }
00671
00675 void DisablePVSOnly () { use_pvs_only = false; }
00676
00680 bool IsPVSOnly () { return use_pvs_only; }
00681
00685 void FreezePVS (const csVector3& pos) { freeze_pvs = true; freeze_pvs_pos = pos; }
00686
00690 void UnfreezePVS () { freeze_pvs = false; }
00691
00695 bool IsPVSFrozen () { return freeze_pvs; }
00696
00700 const csVector3& GetFrozenPosition () const { return freeze_pvs_pos; }
00701
00706 virtual void SetLightingCacheMode (int mode) { lightcache_mode = mode; }
00708 virtual int GetLightingCacheMode () { return lightcache_mode; }
00709
00711 virtual int GetLightmapCellSize () const;
00713 virtual void SetLightmapCellSize (int Size);
00715 virtual int GetDefaultLightmapCellSize () const { return default_lightmap_cell_size; }
00716
00718 virtual void SetClearZBuf (bool yesno)
00719 {
00720 clear_zbuf = yesno;
00721 }
00722
00724 virtual bool GetClearZBuf () const { return clear_zbuf; }
00725
00727 virtual bool GetDefaultClearZBuf () const { return default_clear_zbuf; }
00728
00733 virtual void SetMaxLightmapSize(int w, int h)
00734 { max_lightmap_w = w; max_lightmap_h = h; }
00736 virtual void GetMaxLightmapSize(int& w, int& h)
00737 { w = max_lightmap_w; h = max_lightmap_h; }
00739 virtual void GetDefaultMaxLightmapSize(int& w, int& h)
00740 { w = default_max_lightmap_w; h = default_max_lightmap_h; }
00741
00749 virtual void ResetWorldSpecificSettings();
00750
00756 void ReadConfig (iConfigFile*);
00757
00763 void StartEngine ();
00764
00768 csTextureList* GetTextures () const { return textures; }
00769
00773 csMaterialList* GetMaterials () const { return materials; }
00774
00778 virtual iMaterial* CreateBaseMaterial (iTextureWrapper* txt);
00779 virtual iMaterial* CreateBaseMaterial (iTextureWrapper* txt,
00780 int num_layers, iTextureWrapper** wrappers, csTextureLayer* layers);
00781
00782 virtual iMaterialList* GetMaterialList () const;
00783 virtual iTextureList* GetTextureList () const;
00784 virtual iRegionList* GetRegions ();
00785
00791 iMeshWrapper* CreateSectorWallsMesh (csSector* sector, const char* name);
00792
00798 virtual iMeshWrapper* CreateSectorWallsMesh (iSector* sector,
00799 const char* name);
00800
00804 void AddDynLight (csDynLight* dyn);
00805
00809 void RemoveDynLight (csDynLight* dyn);
00810
00814 csDynLight* GetFirstDynLight () const { return first_dyn_lights; }
00815
00834 virtual int GetNearbyLights (iSector* sector, const csVector3& pos,
00835 uint32 flags, iLight** lights, int max_num_lights);
00836
00842 virtual iSectorIterator* GetNearbySectors (iSector* sector,
00843 const csVector3& pos, float radius);
00844
00852 virtual iObjectIterator* GetNearbyObjects (iSector* sector,
00853 const csVector3& pos, float radius);
00854
00855 virtual bool RemoveObject (iBase* object);
00856
00860 void AddHalo (csLight* Light);
00861
00865 void RemoveHalo (csLight* Light);
00866
00874 virtual void Draw (iCamera* c, iClipper2D* clipper);
00875
00883 virtual void DrawFunc (iCamera* c, iClipper2D* clipper,
00884 iDrawFuncCallback* callback);
00885
00889 csLightIt* NewLightIterator (iRegion* region = NULL)
00890 {
00891 csLightIt* it;
00892 it = new csLightIt (this, region);
00893 return it;
00894 }
00895
00899 void AddToCurrentRegion (csObject* obj);
00900
00902 virtual void RegisterRenderPriority (const char* name, long priority,
00903 int rendsort = CS_RENDPRI_NONE);
00905 virtual long GetRenderPriority (const char* name) const;
00907 virtual int GetRenderPrioritySorting (const char* name) const;
00909 virtual int GetRenderPrioritySorting (long priority) const;
00911 virtual long GetSkyRenderPriority () const { return render_priority_sky; }
00913 virtual long GetWallRenderPriority () const { return render_priority_wall; }
00915 virtual long GetObjectRenderPriority () const { return render_priority_object; }
00917 virtual long GetAlphaRenderPriority () const { return render_priority_alpha; }
00919 virtual void ClearRenderPriorities ();
00920
00922 csThingObjectType* thing_type;
00923 virtual iMeshObjectType* GetThingType () const
00924 {
00925 return (iMeshObjectType*)thing_type;
00926 }
00927
00928 SCF_DECLARE_IBASE;
00929
00930
00931
00937 virtual bool Initialize (iObjectRegistry* object_reg);
00938
00940 virtual bool HandleEvent (iEvent &Event);
00941
00942 struct eiComponent : public iComponent
00943 {
00944 SCF_DECLARE_EMBEDDED_IBASE(csEngine);
00945 virtual bool Initialize (iObjectRegistry* p)
00946 { return scfParent->Initialize(p); }
00947 } scfiComponent;
00948 struct EventHandler : public iEventHandler
00949 {
00950 private:
00951 csEngine* parent;
00952 public:
00953 SCF_DECLARE_IBASE;
00954 EventHandler (csEngine* parent)
00955 {
00956 SCF_CONSTRUCT_IBASE (NULL);
00957 EventHandler::parent = parent;
00958 }
00959 virtual bool HandleEvent (iEvent& e) { return parent->HandleEvent(e); }
00960 } * scfiEventHandler;
00961
00962
00963
00964 virtual csEngine *GetCsEngine () { return this; };
00965
00970 virtual int GetTextureFormat () const;
00971
00976 virtual void SelectRegion (const char* iName);
00977
00982 virtual void SelectRegion (iRegion* region);
00983
00988 virtual iRegion* GetCurrentRegion () const;
00989
00991 virtual void DeleteAll ();
00992
00994 virtual iTextureWrapper* CreateTexture (const char *iName, const char *iFileName,
00995 csColor *iTransp, int iFlags);
00997 virtual iMaterialWrapper* CreateMaterial (const char *iName, iTextureWrapper* texture);
00998
01000 virtual iSector *CreateSector (const char *iName);
01001
01003 virtual iSectorList *GetSectors ()
01004 { return §ors.scfiSectorList; }
01006 virtual iMeshFactoryList *GetMeshFactories ()
01007 { return &mesh_factories.scfiMeshFactoryList; }
01009 virtual iMeshList *GetMeshes ()
01010 { return &meshes.scfiMeshList; }
01012 virtual iCollectionList *GetCollections ()
01013 { return &collections.scfiCollectionList; }
01015 virtual iCameraPositionList *GetCameraPositions ()
01016 { return &camera_positions.scfiCameraPositionList; }
01017
01019 virtual iCamera* CreateCamera ();
01020
01022 virtual iStatLight* CreateLight (const char* name,
01023 const csVector3& pos, float radius,
01024 const csColor& color, bool pseudoDyn);
01026 virtual iStatLight* FindLight (unsigned long light_id) const;
01028 virtual iStatLight* FindLight (const char *Name, bool RegionOnly = false)
01029 const;
01031 virtual iDynLight* CreateDynLight (const csVector3& pos, float radius,
01032 const csColor& color);
01034 virtual void RemoveDynLight (iDynLight*);
01035
01037 virtual iMeshFactoryWrapper* CreateMeshFactory (const char* classId,
01038 const char* name);
01040 virtual iMeshFactoryWrapper* CreateMeshFactory (iMeshObjectFactory *,
01041 const char* name);
01043 virtual iMeshFactoryWrapper* CreateMeshFactory (const char* name);
01044 virtual iLoaderContext* CreateLoaderContext (iRegion* region = NULL);
01046 virtual iMeshFactoryWrapper* LoadMeshFactory (
01047 const char* name, const char* loaderClassId,
01048 iDataBuffer* input);
01049
01051 virtual iMeshWrapper* CreateMeshWrapper (iMeshFactoryWrapper* factory,
01052 const char* name, iSector* sector = NULL,
01053 const csVector3& pos = csVector3(0, 0, 0));
01055 virtual iMeshWrapper* CreateMeshWrapper (iMeshObject*,
01056 const char* name, iSector* sector = NULL,
01057 const csVector3& pos = csVector3(0, 0, 0));
01059 virtual iMeshWrapper* CreateMeshWrapper (const char* name);
01061 virtual iMeshWrapper* LoadMeshWrapper (
01062 const char* name, const char* loaderClassId,
01063 iDataBuffer* input, iSector* sector, const csVector3& pos);
01064
01065 virtual iClipper2D* GetTopLevelClipper () const;
01066
01068 virtual void SetAmbientLight (const csColor &c);
01070 virtual void GetAmbientLight (csColor &c) const;
01071
01072 virtual iMaterialWrapper* FindMaterial (const char* name,
01073 iRegion* region = NULL);
01074 virtual iTextureWrapper* FindTexture (const char* name,
01075 iRegion* region = NULL);
01076 virtual iSector* FindSector (const char* name,
01077 iRegion* region = NULL);
01078 virtual iMeshWrapper* FindMeshObject (const char* name,
01079 iRegion* region = NULL);
01080 virtual iMeshFactoryWrapper* FindMeshFactory (const char* name,
01081 iRegion* region = NULL);
01082 virtual iCameraPosition* FindCameraPosition (const char* name,
01083 iRegion* region = NULL);
01084 virtual iCollection* FindCollection (const char* name,
01085 iRegion* region = NULL);
01086
01087
01088
01089 virtual void SetCacheManager (iCacheManager* cache_mgr);
01090 virtual iCacheManager* GetCacheManager ();
01091
01092
01093
01094 csEngineConfig scfiConfig;
01095
01096
01097
01099 virtual void SetContext (iGraphics3D* g3d);
01101 virtual iGraphics3D *GetContext () const;
01102
01103 private:
01105 void Resize ();
01107 bool resize;
01113 class csEngineState
01114 {
01115 public:
01116 csEngine *engine;
01117 bool resize;
01118 iGraphics2D *G2D;
01119 iGraphics3D *G3D;
01120 csCBuffer* c_buffer;
01121 csCBufferCube* cbufcube;
01123 csEngineState (csEngine *this_engine);
01124
01126 virtual ~csEngineState ();
01127
01129 void Activate ();
01130 };
01131
01132 friend class csEngineState;
01133
01134 class csEngineStateVector : public csVector
01135 {
01136 public:
01137
01138 csEngineStateVector () : csVector (8, 8) {}
01139
01140 virtual ~csEngineStateVector () { DeleteAll (); }
01141
01142 virtual bool FreeItem (csSome Item)
01143 { delete (csEngineState *)Item; return true; }
01144
01145 virtual int CompareKey (csSome Item, csConstSome Key, int ) const
01146 { return ((csEngineState *)Item)->G3D == (iGraphics3D *)Key ? 0 : -1; }
01147
01148 inline csEngineState *Get (int n) const
01149 { return (csEngineState *)csVector::Get (n); }
01150
01151
01152 void Resize (iGraphics2D *g2d);
01153
01154
01155 void Close (iGraphics2D *g2d);
01156 };
01157
01158 csEngineStateVector *engine_states;
01159
01160
01161
01165 class iObjectInterface : public csObject
01166 {
01167 SCF_DECLARE_EMBEDDED_IBASE (csEngine);
01168 } scfiObject;
01169 };
01170
01171 #endif // __CS_ENGINE_H__