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 "csgeom/math3d.h"
00024 #include "csutil/nobjvec.h"
00025 #include "csengine/arrays.h"
00026 #include "csengine/rview.h"
00027 #include "csengine/thing.h"
00028 #include "csengine/meshobj.h"
00029 #include "csutil/csobject.h"
00030 #include "ivideo/graph3d.h"
00031 #include "iutil/eventh.h"
00032 #include "iutil/comp.h"
00033 #include "iengine/engine.h"
00034 #include "iengine/collectn.h"
00035 #include "iengine/campos.h"
00036 #include "iutil/config.h"
00037
00038 class csRegion;
00039 class csRadiosity;
00040 class csSector;
00041 class csMeshWrapper;
00042 class csTextureList;
00043 class csMaterialList;
00044 class csPolygon3D;
00045 class csCamera;
00046 class csStatLight;
00047 class csDynLight;
00048 class csCBufferCube;
00049 class csEngine;
00050 class csLight;
00051 class csCBuffer;
00052 class csPoly2DPool;
00053 class csLightPatchPool;
00054 class csLightHalo;
00055 class csRenderView;
00056 struct iVFS;
00057 struct iMaterialWrapper;
00058 struct iRegion;
00059 struct iLight;
00060 struct iImageIO;
00061 struct iClipper2D;
00062 struct iReporter;
00063 struct iProgressMeter;
00064 struct iObjectRegistry;
00065 struct iPluginManager;
00066
00067 SCF_DECLARE_FAST_INTERFACE (iEngine)
00068 SCF_DECLARE_FAST_INTERFACE (iSector)
00069 SCF_DECLARE_FAST_INTERFACE (iMeshWrapper)
00070 SCF_DECLARE_FAST_INTERFACE (iCollection)
00071 SCF_DECLARE_FAST_INTERFACE (iMeshFactoryWrapper)
00072 SCF_DECLARE_FAST_INTERFACE (iCurveTemplate)
00073 SCF_DECLARE_FAST_INTERFACE (iMaterialWrapper)
00074 SCF_DECLARE_FAST_INTERFACE (iTextureWrapper)
00075 SCF_DECLARE_FAST_INTERFACE (iCameraPosition)
00076 SCF_DECLARE_FAST_INTERFACE (iPolyTxtPlane)
00077 SCF_DECLARE_FAST_INTERFACE (iStatLight)
00078 SCF_DECLARE_FAST_INTERFACE (iDynLight)
00079 SCF_DECLARE_FAST_INTERFACE (iLightingInfo)
00080 SCF_DECLARE_FAST_INTERFACE (iLight)
00081 SCF_DECLARE_FAST_INTERFACE (iMeshObject)
00082 SCF_DECLARE_FAST_INTERFACE (iMaterial)
00083 SCF_DECLARE_FAST_INTERFACE (iCrossHalo)
00084 SCF_DECLARE_FAST_INTERFACE (iNovaHalo)
00085 SCF_DECLARE_FAST_INTERFACE (iFlareHalo)
00086 SCF_DECLARE_FAST_INTERFACE (iThingState)
00087 SCF_DECLARE_FAST_INTERFACE (iShadowCaster)
00088 SCF_DECLARE_FAST_INTERFACE (iPolygon3D)
00089 SCF_DECLARE_FAST_INTERFACE (iMeshObjectFactory)
00090 SCF_DECLARE_FAST_INTERFACE (iReferencedObject)
00091 SCF_DECLARE_FAST_INTERFACE (iFile)
00092 SCF_DECLARE_FAST_INTERFACE (iVisibilityObject)
00093 SCF_DECLARE_FAST_INTERFACE (iVisibilityCuller)
00094
00095 SCF_DECLARE_FAST_INTERFACE (csRadPoly)
00096 SCF_DECLARE_FAST_INTERFACE (csRadCurve)
00097
00104 class csLightIt
00105 {
00106 private:
00107
00108 csEngine* engine;
00109
00110 iRegion* region;
00111
00112 int sector_idx;
00113
00114 int light_idx;
00115
00116
00117 bool NextSector ();
00118
00119 public:
00121 csLightIt (csEngine*, iRegion* region = NULL);
00122
00124 void Restart ();
00125
00127 csLight* Fetch ();
00128
00130 csSector* GetLastSector ();
00131 };
00132
00140 class csSectorIt
00141 {
00142 private:
00143
00144 csSector* sector;
00145 csVector3 pos;
00146 float radius;
00147
00148
00149 int cur_poly;
00150
00151
00152 csSectorIt* recursive_it;
00153
00154 bool has_ended;
00155
00156 csVector3 last_pos;
00157
00158 public:
00160 csSectorIt (csSector* sector, const csVector3& pos, float radius);
00161
00163 ~csSectorIt ();
00164
00166 void Restart ();
00167
00169 csSector* Fetch ();
00170
00175 const csVector3& GetLastPosition () { return last_pos; }
00176 };
00177
00184 class csObjectIt
00185 {
00186 friend class csEngine;
00187
00188 private:
00189
00190 csEngine* engine;
00191
00192 csSector* start_sector;
00193 csVector3 start_pos;
00194 float radius;
00195
00196 csSector* cur_sector;
00197 csVector3 cur_pos;
00198
00199 iObject* cur_object;
00200
00201 int cur_idx;
00202
00203 csSectorIt* sectors_it;
00204
00205 enum {
00206 ITERATE_SECTORS,
00207 ITERATE_STATLIGHTS,
00208 ITERATE_DYNLIGHTS,
00209 ITERATE_MESHES,
00210 ITERATE_NONE
00211 } CurrentList;
00212
00213 private:
00214
00215 void StartStatLights ();
00216 void StartMeshes ();
00217 void EndSearch ();
00218
00220 csObjectIt (csEngine*, csSector* sector,
00221 const csVector3& pos, float radius);
00222
00223 public:
00225 ~csObjectIt ();
00226
00228 void Restart ();
00229
00231 iObject* Fetch ();
00232 };
00233
00234 CS_DECLARE_OBJECT_VECTOR (csCollectionListHelper, iCollection);
00235
00239 class csCollectionList : public csCollectionListHelper
00240 {
00241 public:
00242 SCF_DECLARE_IBASE;
00243
00245 csCollectionList ();
00247 virtual iCollection* NewCollection (const char* name);
00248
00249 class CollectionList : public iCollectionList
00250 {
00251 public:
00252 SCF_DECLARE_EMBEDDED_IBASE (csCollectionList);
00253 virtual iCollection* NewCollection (const char* name);
00254 virtual int GetCount () const;
00255 virtual iCollection *Get (int n) const;
00256 virtual int Add (iCollection *obj);
00257 virtual bool Remove (iCollection *obj);
00258 virtual bool Remove (int n);
00259 virtual void RemoveAll ();
00260 virtual int Find (iCollection *obj) const;
00261 virtual iCollection *FindByName (const char *Name) const;
00262 } scfiCollectionList;
00263 };
00264
00265 CS_DECLARE_OBJECT_VECTOR (csCameraPositionListHelper, iCameraPosition);
00266
00271 class csCameraPositionList : public csCameraPositionListHelper
00272 {
00273 public:
00274 SCF_DECLARE_IBASE;
00275
00277 csCameraPositionList ();
00279 virtual iCameraPosition* NewCameraPosition (const char* name);
00280
00281 class CameraPositionList : public iCameraPositionList
00282 {
00283 SCF_DECLARE_EMBEDDED_IBASE (csCameraPositionList);
00284 virtual iCameraPosition* NewCameraPosition (const char* name);
00285 virtual int GetCount () const;
00286 virtual iCameraPosition *Get (int n) const;
00287 virtual int Add (iCameraPosition *obj);
00288 virtual bool Remove (iCameraPosition *obj);
00289 virtual bool Remove (int n);
00290 virtual void RemoveAll ();
00291 virtual int Find (iCameraPosition *obj) const;
00292 virtual iCameraPosition *FindByName (const char *Name) const;
00293 } scfiCameraPositionList;
00294 };
00295
00299 class csEngineMeshList : public csMeshList
00300 {
00301 public:
00302 virtual ~csEngineMeshList ();
00303 virtual bool FreeItem (csSome Item);
00304 virtual void RemoveMesh (iMeshWrapper* mesh);
00305 };
00306
00318 struct csEngineConfig : public iConfig
00319 {
00320 SCF_DECLARE_EMBEDDED_IBASE (csEngine);
00321 virtual bool GetOptionDescription (int idx, csOptionDescription *option);
00322 virtual bool SetOption (int id, csVariant* value);
00323 virtual bool GetOption (int id, csVariant* value);
00324 };
00325
00331 class csEngine : public iEngine
00332 {
00333 public:
00342 iVFS *VFS;
00343
00348 iReporter* Reporter;
00349
00361 csObjVector cleanup;
00362
00368 csSectorList sectors;
00369
00374 csCollectionList collections;
00375
00380 csMeshFactoryList mesh_factories;
00381
00389 csEngineMeshList meshes;
00390
00394 csCameraPositionList camera_positions;
00395
00397 static int frame_width, frame_height;
00399 static iObjectRegistry* object_reg;
00401 static iPluginManager* plugin_mgr;
00403 static csEngine* current_engine;
00405 static iEngine* current_iengine;
00407 static bool use_new_radiosity;
00409 csPoly2DPool* render_pol2d_pool;
00411 csLightPatchPool* lightpatch_pool;
00413 iGraphics3D* G3D;
00415 iGraphics2D* G2D;
00417 iImageIO* ImageLoader;
00419 G3D_FOGMETHOD fogmethod;
00421 bool NeedPO2Maps;
00423 int MaxAspectRatio;
00425 iRegion* region;
00427 csNamedObjVector regions;
00428
00430 csVector render_priorities;
00441 long render_priority_sky;
00442 long render_priority_wall;
00443 long render_priority_object;
00444 long render_priority_alpha;
00445
00447 static int lightcache_mode;
00449 static int lightmap_quality;
00451 static bool do_force_revis;
00453 static bool do_rad_debug;
00454
00455 private:
00457 csTextureList* textures;
00459 csMaterialList* materials;
00461 csDynLight* first_dyn_lights;
00463 csHaloArray halos;
00465 static int max_process_polygons;
00467 static int cur_process_polygons;
00468
00470 int engine_mode;
00471
00473 csRadiosity* rad_debug;
00474
00476 csCBuffer* c_buffer;
00477
00479 csCBufferCube* cbufcube;
00480
00482 bool use_pvs;
00483
00488 bool use_pvs_only;
00489
00496 bool freeze_pvs;
00498 csVector3 freeze_pvs_pos;
00499
00508 csTicks nextframe_pending;
00509
00510 private:
00514 void ResolveEngineMode ();
00515
00519 void StartDraw (iCamera* c, iClipper2D* view, csRenderView& rview);
00520
00524 void ControlMeshes ();
00525
00526 public:
00530 iCamera* current_camera;
00531
00535 iClipper2D* top_clipper;
00536
00537 public:
00543 csEngine (iBase *iParent);
00544
00551 virtual ~csEngine ();
00552
00556 void Report (const char* description, ...);
00557
00561 void Warn (const char* description, ...);
00562
00566 void ReportBug (const char* description, ...);
00567
00575 bool CheckConsistency ();
00576
00584 void PrepareTextures ();
00585
00591 void PrepareMeshes ();
00592
00599 virtual void ShineLights (iRegion* region = NULL,
00600 iProgressMeter* meter = NULL);
00601
00603 virtual iObject *QueryObject();
00605 inline csObject *QueryCsObject () { return &scfiObject; }
00606
00615 virtual bool Prepare (iProgressMeter* meter = NULL);
00616
00621 static void SetMaxProcessPolygons (int m) { max_process_polygons = m; }
00622
00626 static int GetMaxProcessPolygons () { return max_process_polygons; }
00627
00632 static bool ProcessPolygon ()
00633 {
00634 if (cur_process_polygons > max_process_polygons) return false;
00635 cur_process_polygons++;
00636 return true;
00637 }
00638
00642 static bool ProcessLastPolygon ()
00643 {
00644 return cur_process_polygons >= max_process_polygons;
00645 }
00646
00651 csRadiosity* GetRadiosity () const { return rad_debug; }
00652
00658 void InvalidateLightmaps ();
00659
00667 virtual void SetEngineMode (int mode)
00668 {
00669 engine_mode = mode;
00670 }
00671
00677 virtual int GetEngineMode () const { return engine_mode; }
00678
00684 virtual int GetBeginDrawFlags () const
00685 {
00686 if (engine_mode == CS_ENGINE_ZBUFFER)
00687 return CSDRAW_CLEARZBUFFER;
00688 else
00689 return 0;
00690 }
00691
00695 csTicks GetLastAnimationTime () const { return nextframe_pending; }
00696
00700 void InitCuller ();
00701
00705 csCBuffer* GetCBuffer () const { return c_buffer; }
00706
00710 csCBufferCube* GetCBufCube () const { return cbufcube; }
00711
00715 void EnablePVS () { use_pvs = true; use_pvs_only = false; }
00716
00720 void DisablePVS () { use_pvs = false; }
00721
00725 virtual bool IsPVS () const { return use_pvs; }
00726
00731 void EnablePVSOnly () { use_pvs_only = true; }
00732
00736 void DisablePVSOnly () { use_pvs_only = false; }
00737
00741 bool IsPVSOnly () { return use_pvs_only; }
00742
00746 void FreezePVS (const csVector3& pos) { freeze_pvs = true; freeze_pvs_pos = pos; }
00747
00751 void UnfreezePVS () { freeze_pvs = false; }
00752
00756 bool IsPVSFrozen () { return freeze_pvs; }
00757
00761 const csVector3& GetFrozenPosition () const { return freeze_pvs_pos; }
00762
00767 virtual void SetLightingCacheMode (int mode) { lightcache_mode = mode; }
00769 virtual int GetLightingCacheMode () { return lightcache_mode; }
00770
00772 virtual int GetLightmapCellSize () const;
00774 virtual void SetLightmapCellSize (int Size);
00775
00781 void ReadConfig (iConfigFile*);
00782
00788 void StartEngine ();
00789
00793 csTextureList* GetTextures () const { return textures; }
00794
00798 csMaterialList* GetMaterials () const { return materials; }
00799
00803 virtual iMaterial* CreateBaseMaterial (iTextureWrapper* txt);
00804 virtual iMaterial* CreateBaseMaterial (iTextureWrapper* txt,
00805 int num_layers, iTextureWrapper** wrappers, csTextureLayer* layers);
00806
00807 virtual iMaterialList* GetMaterialList () const;
00808 virtual iTextureList* GetTextureList () const;
00809
00815 iMeshWrapper* CreateSectorWallsMesh (csSector* sector, const char* name);
00816
00822 virtual iMeshWrapper* CreateSectorWallsMesh (iSector* sector,
00823 const char* name);
00824
00828 void AddDynLight (csDynLight* dyn);
00829
00833 void RemoveDynLight (csDynLight* dyn);
00834
00838 csDynLight* GetFirstDynLight () const { return first_dyn_lights; }
00839
00858 int GetNearbyLights (csSector* sector, const csVector3& pos, ULong flags,
00859 iLight** lights, int max_num_lights);
00860
00866 csSectorIt* GetNearbySectors (csSector* sector,
00867 const csVector3& pos, float radius);
00868
00876 csObjectIt* GetNearbyObjects (csSector* sector,
00877 const csVector3& pos, float radius);
00878
00882 void AddHalo (csLight* Light);
00883
00887 void RemoveHalo (csLight* Light);
00888
00896 virtual void Draw (iCamera* c, iClipper2D* clipper);
00897
00905 virtual void DrawFunc (iCamera* c, iClipper2D* clipper,
00906 iDrawFuncCallback* callback);
00907
00913 csStatLight* FindCsLight (float x, float y, float z, float dist) const;
00914
00918 csStatLight* FindCsLight (unsigned long id) const;
00919
00923 csStatLight* FindCsLight (const char* name, bool regionOnly = false) const;
00924
00928 csLightIt* NewLightIterator (iRegion* region = NULL)
00929 {
00930 csLightIt* it;
00931 it = new csLightIt (this, region);
00932 return it;
00933 }
00934
00938 void AddToCurrentRegion (csObject* obj);
00939
00941 virtual void RegisterRenderPriority (const char* name, long priority);
00943 virtual long GetRenderPriority (const char* name) const;
00945 virtual long GetSkyRenderPriority () const { return render_priority_sky; }
00947 virtual long GetWallRenderPriority () const { return render_priority_wall; }
00949 virtual long GetObjectRenderPriority () const { return render_priority_object; }
00951 virtual long GetAlphaRenderPriority () const { return render_priority_alpha; }
00953 virtual void ClearRenderPriorities ();
00954
00956 csThingObjectType* thing_type;
00957 virtual iMeshObjectType* GetThingType () const
00958 {
00959 return (iMeshObjectType*)thing_type;
00960 }
00961
00962 SCF_DECLARE_IBASE;
00963
00964
00965
00971 virtual bool Initialize (iObjectRegistry* object_reg);
00972
00974 virtual bool HandleEvent (iEvent &Event);
00975
00976 struct eiComponent : public iComponent
00977 {
00978 SCF_DECLARE_EMBEDDED_IBASE(csEngine);
00979 virtual bool Initialize (iObjectRegistry* p)
00980 { return scfParent->Initialize(p); }
00981 } scfiComponent;
00982 struct eiEventHandler : public iEventHandler
00983 {
00984 SCF_DECLARE_EMBEDDED_IBASE(csEngine);
00985 virtual bool HandleEvent (iEvent& e) { return scfParent->HandleEvent(e); }
00986 } scfiEventHandler;
00987
00988
00989
00990 virtual csEngine *GetCsEngine () { return this; };
00991
00996 virtual int GetTextureFormat () const;
00997
01002 virtual void SelectRegion (const char* iName);
01003
01008 virtual void SelectRegion (iRegion* region);
01009
01014 virtual iRegion* GetCurrentRegion () const;
01015
01017 virtual iRegion* FindRegion (const char *name) const;
01018
01020 virtual void DeleteAll ();
01021
01023 virtual iTextureWrapper* CreateTexture (const char *iName, const char *iFileName,
01024 csColor *iTransp, int iFlags);
01026 virtual iMaterialWrapper* CreateMaterial (const char *iName, iTextureWrapper* texture);
01027
01029 virtual iSector *CreateSector (const char *iName, bool link = true);
01030
01032 virtual iSectorList *GetSectors ()
01033 { return §ors.scfiSectorList; }
01035 virtual iMeshFactoryList *GetMeshFactories ()
01036 { return &mesh_factories.scfiMeshFactoryList; }
01038 virtual iMeshList *GetMeshes ()
01039 { return &meshes.scfiMeshList; }
01041 virtual iCollectionList *GetCollections ()
01042 { return &collections.scfiCollectionList; }
01044 virtual iCameraPositionList *GetCameraPositions ()
01045 { return &camera_positions.scfiCameraPositionList; }
01046
01048 virtual iCamera* CreateCamera ();
01049
01051 virtual iStatLight* CreateLight (const char* name,
01052 const csVector3& pos, float radius,
01053 const csColor& color, bool pseudoDyn);
01055 virtual iStatLight* FindLight (const char *Name, bool RegionOnly = false)
01056 const;
01058 virtual iDynLight* CreateDynLight (const csVector3& pos, float radius,
01059 const csColor& color);
01061 virtual void RemoveDynLight (iDynLight*);
01062
01064 virtual iMeshFactoryWrapper* CreateMeshFactory (const char* classId,
01065 const char* name);
01067 virtual iMeshFactoryWrapper* CreateMeshFactory (iMeshObjectFactory *,
01068 const char* name);
01070 virtual iMeshFactoryWrapper* CreateMeshFactory (const char* name);
01072 virtual iMeshFactoryWrapper* LoadMeshFactory (
01073 const char* classId, const char* name,
01074 const char* loaderClassId,
01075 iDataBuffer* input);
01076
01078 virtual iMeshWrapper* CreateMeshWrapper (iMeshFactoryWrapper* factory,
01079 const char* name, iSector* sector = NULL,
01080 const csVector3& pos = csVector3(0, 0, 0));
01082 virtual iMeshWrapper* CreateMeshWrapper (iMeshObject*,
01083 const char* name, iSector* sector = NULL,
01084 const csVector3& pos = csVector3(0, 0, 0));
01086 virtual iMeshWrapper* CreateMeshWrapper (const char* name);
01088 virtual iMeshWrapper* LoadMeshWrapper (
01089 const char* classId, const char* name,
01090 const char* loaderClassId,
01091 iDataBuffer* input, iSector* sector, const csVector3& pos);
01092
01093 virtual iClipper2D* GetTopLevelClipper () const;
01094
01096 virtual void SetAmbientLight (const csColor &c);
01098 virtual void GetAmbientLight (csColor &c) const;
01099
01100
01101
01102 csEngineConfig scfiConfig;
01103
01104
01105
01107 virtual void SetContext (iGraphics3D* g3d);
01109 virtual iGraphics3D *GetContext () const;
01110
01111 private:
01113 void Resize ();
01115 bool resize;
01121 class csEngineState
01122 {
01123 public:
01124 csEngine *engine;
01125 bool resize;
01126 iGraphics2D *G2D;
01127 iGraphics3D *G3D;
01128 csCBuffer* c_buffer;
01129 csCBufferCube* cbufcube;
01131 csEngineState (csEngine *this_engine);
01132
01134 virtual ~csEngineState ();
01135
01137 void Activate ();
01138 };
01139
01140 friend class csEngineState;
01141
01142 class csEngineStateVector : public csVector
01143 {
01144 public:
01145
01146 csEngineStateVector () : csVector (8, 8) {}
01147
01148 virtual ~csEngineStateVector () { DeleteAll (); }
01149
01150 virtual bool FreeItem (csSome Item)
01151 { delete (csEngineState *)Item; return true; }
01152
01153 virtual int CompareKey (csSome Item, csConstSome Key, int ) const
01154 { return ((csEngineState *)Item)->G3D == (iGraphics3D *)Key ? 0 : -1; }
01155
01156 inline csEngineState *Get (int n) const
01157 { return (csEngineState *)csVector::Get (n); }
01158
01159
01160 void Resize (iGraphics2D *g2d);
01161
01162
01163 void Close (iGraphics2D *g2d);
01164 };
01165
01166 csEngineStateVector *engine_states;
01167
01168
01169
01173 class iObjectInterface : public csObject
01174 {
01175 SCF_DECLARE_EMBEDDED_IBASE (csEngine);
01176 } scfiObject;
01177 };
01178
01179 #endif // __CS_ENGINE_H__