Google

Main Page   Class Hierarchy   Compound List   File List   Compound Members  

engine.h

00001 /*
00002     Copyright (C) 1998-2001 by Jorrit Tyberghein
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public
00015     License along with this library; if not, write to the Free
00016     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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   // The engine for this iterator.
00111   csEngine* engine;
00112   // The region we are iterating in (optional).
00113   iRegion* region;
00114   // Current sector index.
00115   int sector_idx;
00116   // Current light index.
00117   int light_idx;
00118 
00119   // Go to next sector. Return false if finished.
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;
00355 
00356 private:
00358   csTextureList* textures;
00360   csMaterialList* materials;
00362   csDynLight* first_dyn_lights;
00364   csHaloArray halos;
00366   static int max_process_polygons;
00368   static int cur_process_polygons;
00369 
00371   int engine_mode;
00372 
00374   csRadiosity* rad_debug;
00375 
00377   csCBuffer* c_buffer;
00378 
00380   csCBufferCube* cbufcube;
00381 
00383   bool use_pvs;
00384 
00389   bool use_pvs_only;
00390 
00397   bool freeze_pvs;
00399   csVector3 freeze_pvs_pos;
00400 
00402   bool clear_zbuf;
00403 
00412   csTicks nextframe_pending;
00413 
00415   iVirtualClock* virtual_clock;
00416 
00417 private:
00421   void ResolveEngineMode ();
00422 
00426   void StartDraw (iCamera* c, iClipper2D* view, csRenderView& rview);
00427 
00431   void ControlMeshes ();
00432 
00444   char* SplitRegionName (const char* name, iRegion*& region, bool& global);
00445 
00451   iCacheManager* cache_mgr;
00452 
00453 public:
00457   iCamera* current_camera;
00458 
00462   iClipper2D* top_clipper;
00463 
00464 public:
00470   csEngine (iBase *iParent);
00471 
00478   virtual ~csEngine ();
00479 
00483   void Report (const char* description, ...) CS_GNUC_PRINTF (2, 3);
00484 
00488   void Warn (const char* description, ...) CS_GNUC_PRINTF (2, 3);
00489 
00493   void ReportBug (const char* description, ...) CS_GNUC_PRINTF (2, 3);
00494 
00502   bool CheckConsistency ();
00503 
00511   virtual void PrepareTextures ();
00512 
00518   virtual void PrepareMeshes ();
00519 
00526   virtual void ShineLights (iRegion* region = NULL,
00527         iProgressMeter* meter = NULL);
00528 
00530   virtual iObject *QueryObject();
00532   inline csObject *QueryCsObject () { return &scfiObject; }
00533 
00542   virtual bool Prepare (iProgressMeter* meter = NULL);
00543 
00548   static void SetMaxProcessPolygons (int m) { max_process_polygons = m; }
00549 
00553   static int GetMaxProcessPolygons () { return max_process_polygons; }
00554 
00559   static bool ProcessPolygon ()
00560   {
00561     if (cur_process_polygons > max_process_polygons) return false;
00562     cur_process_polygons++;
00563     return true;
00564   }
00565 
00569   static bool ProcessLastPolygon ()
00570   {
00571     return cur_process_polygons >= max_process_polygons;
00572   }
00573 
00578   csRadiosity* GetRadiosity () const { return rad_debug; }
00579 
00585   void InvalidateLightmaps ();
00586 
00594   virtual void SetEngineMode (int mode)
00595   {
00596     engine_mode = mode;
00597   }
00598 
00604   virtual int GetEngineMode () const { return engine_mode; }
00605 
00606   virtual void SetClearZBuf (bool yesno)
00607   {
00608     clear_zbuf = yesno;
00609   }
00610 
00611   virtual bool GetClearZBuf () const { return clear_zbuf; }
00612 
00618   virtual int GetBeginDrawFlags () const
00619   {
00620     if (clear_zbuf || engine_mode == CS_ENGINE_ZBUFFER)
00621       return CSDRAW_CLEARZBUFFER;
00622     else
00623       return 0;
00624   }
00625 
00629   csTicks GetLastAnimationTime () const { return nextframe_pending; }
00630 
00634   void InitCuller ();
00635 
00639   csCBuffer* GetCBuffer () const { return c_buffer; }
00640 
00644   csCBufferCube* GetCBufCube () const { return cbufcube; }
00645 
00649   void EnablePVS () { use_pvs = true; use_pvs_only = false; }
00650 
00654   void DisablePVS () { use_pvs = false; }
00655 
00659   virtual bool IsPVS () const { return use_pvs; }
00660 
00665   void EnablePVSOnly () { use_pvs_only = true; }
00666 
00670   void DisablePVSOnly () { use_pvs_only = false; }
00671 
00675   bool IsPVSOnly () { return use_pvs_only; }
00676 
00680   void FreezePVS (const csVector3& pos) { freeze_pvs = true; freeze_pvs_pos = pos; }
00681 
00685   void UnfreezePVS () { freeze_pvs = false; }
00686 
00690   bool IsPVSFrozen () { return freeze_pvs; }
00691 
00695   const csVector3& GetFrozenPosition () const { return freeze_pvs_pos; }
00696 
00701   virtual void SetLightingCacheMode (int mode) { lightcache_mode = mode; }
00703   virtual int GetLightingCacheMode () { return lightcache_mode; }
00704 
00706   virtual int GetLightmapCellSize () const;
00708   virtual void SetLightmapCellSize (int Size);
00709 
00715   void ReadConfig (iConfigFile*);
00716 
00722   void StartEngine ();
00723 
00727   csTextureList* GetTextures () const { return textures; }
00728 
00732   csMaterialList* GetMaterials () const { return materials; }
00733 
00737   virtual iMaterial* CreateBaseMaterial (iTextureWrapper* txt);
00738   virtual iMaterial* CreateBaseMaterial (iTextureWrapper* txt,
00739         int num_layers, iTextureWrapper** wrappers, csTextureLayer* layers);
00740 
00741   virtual iMaterialList* GetMaterialList () const;
00742   virtual iTextureList* GetTextureList () const;
00743   virtual iRegionList* GetRegions ();
00744 
00750   iMeshWrapper* CreateSectorWallsMesh (csSector* sector, const char* name);
00751 
00757   virtual iMeshWrapper* CreateSectorWallsMesh (iSector* sector,
00758       const char* name);
00759 
00763   void AddDynLight (csDynLight* dyn);
00764 
00768   void RemoveDynLight (csDynLight* dyn);
00769 
00773   csDynLight* GetFirstDynLight () const { return first_dyn_lights; }
00774 
00793   virtual int GetNearbyLights (iSector* sector, const csVector3& pos,
00794         uint32 flags, iLight** lights, int max_num_lights);
00795 
00801   virtual iSectorIterator* GetNearbySectors (iSector* sector,
00802         const csVector3& pos, float radius);
00803 
00811   virtual iObjectIterator* GetNearbyObjects (iSector* sector,
00812     const csVector3& pos, float radius);
00813 
00814   virtual bool RemoveObject (iBase* object);
00815 
00819   void AddHalo (csLight* Light);
00820 
00824   void RemoveHalo (csLight* Light);
00825 
00833   virtual void Draw (iCamera* c, iClipper2D* clipper);
00834 
00842   virtual void DrawFunc (iCamera* c, iClipper2D* clipper,
00843     iDrawFuncCallback* callback);
00844 
00848   csLightIt* NewLightIterator (iRegion* region = NULL)
00849   {
00850     csLightIt* it;
00851     it = new csLightIt (this, region);
00852     return it;
00853   }
00854 
00858   void AddToCurrentRegion (csObject* obj);
00859 
00861   virtual void RegisterRenderPriority (const char* name, long priority,
00862         int rendsort = CS_RENDPRI_NONE);
00864   virtual long GetRenderPriority (const char* name) const;
00866   virtual int GetRenderPrioritySorting (const char* name) const;
00868   virtual int GetRenderPrioritySorting (long priority) const;
00870   virtual long GetSkyRenderPriority () const { return render_priority_sky; }
00872   virtual long GetWallRenderPriority () const { return render_priority_wall; }
00874   virtual long GetObjectRenderPriority () const { return render_priority_object; }
00876   virtual long GetAlphaRenderPriority () const { return render_priority_alpha; }
00878   virtual void ClearRenderPriorities ();
00879 
00881   csThingObjectType* thing_type;
00882   virtual iMeshObjectType* GetThingType () const
00883   {
00884     return (iMeshObjectType*)thing_type;
00885   }
00886 
00887   SCF_DECLARE_IBASE;
00888 
00889   //------------------ iComponent interface implementation --------------------
00890 
00896   virtual bool Initialize (iObjectRegistry* object_reg);
00897 
00899   virtual bool HandleEvent (iEvent &Event);
00900 
00901   struct eiComponent : public iComponent
00902   {
00903     SCF_DECLARE_EMBEDDED_IBASE(csEngine);
00904     virtual bool Initialize (iObjectRegistry* p)
00905     { return scfParent->Initialize(p); }
00906   } scfiComponent;
00907   struct EventHandler : public iEventHandler
00908   {
00909   private:
00910     csEngine* parent;
00911   public:
00912     SCF_DECLARE_IBASE;
00913     EventHandler (csEngine* parent)
00914     {
00915       SCF_CONSTRUCT_IBASE (NULL);
00916       EventHandler::parent = parent;
00917     }
00918     virtual bool HandleEvent (iEvent& e) { return parent->HandleEvent(e); }
00919   } * scfiEventHandler;
00920 
00921   //--------------------- iEngine interface implementation --------------------
00922 
00923   virtual csEngine *GetCsEngine () { return this; };
00924 
00929   virtual int GetTextureFormat () const;
00930 
00935   virtual void SelectRegion (const char* iName);
00936 
00941   virtual void SelectRegion (iRegion* region);
00942 
00947   virtual iRegion* GetCurrentRegion () const;
00948 
00950   virtual void DeleteAll ();
00951 
00953   virtual iTextureWrapper* CreateTexture (const char *iName, const char *iFileName,
00954     csColor *iTransp, int iFlags);
00956   virtual iMaterialWrapper* CreateMaterial (const char *iName, iTextureWrapper* texture);
00957 
00959   virtual iSector *CreateSector (const char *iName);
00960 
00962   virtual iSectorList *GetSectors ()
00963     { return &sectors.scfiSectorList; }
00965   virtual iMeshFactoryList *GetMeshFactories ()
00966     { return &mesh_factories.scfiMeshFactoryList; }
00968   virtual iMeshList *GetMeshes ()
00969     { return &meshes.scfiMeshList; }
00971   virtual iCollectionList *GetCollections ()
00972     { return &collections.scfiCollectionList; }
00974   virtual iCameraPositionList *GetCameraPositions ()
00975     { return &camera_positions.scfiCameraPositionList; }
00976 
00978   virtual iCamera* CreateCamera ();
00979 
00981   virtual iStatLight* CreateLight (const char* name,
00982         const csVector3& pos, float radius,
00983         const csColor& color, bool pseudoDyn);
00985   virtual iStatLight* FindLight (unsigned long light_id) const;
00987   virtual iStatLight* FindLight (const char *Name, bool RegionOnly = false)
00988     const;
00990   virtual iDynLight* CreateDynLight (const csVector3& pos, float radius,
00991         const csColor& color);
00993   virtual void RemoveDynLight (iDynLight*);
00994 
00996   virtual iMeshFactoryWrapper* CreateMeshFactory (const char* classId,
00997         const char* name);
00999   virtual iMeshFactoryWrapper* CreateMeshFactory (iMeshObjectFactory *,
01000         const char* name);
01002   virtual iMeshFactoryWrapper* CreateMeshFactory (const char* name);
01003   virtual iLoaderContext* CreateLoaderContext (iRegion* region = NULL);
01005   virtual iMeshFactoryWrapper* LoadMeshFactory (
01006         const char* name, const char* loaderClassId,
01007         iDataBuffer* input);
01008 
01010   virtual iMeshWrapper* CreateMeshWrapper (iMeshFactoryWrapper* factory,
01011         const char* name, iSector* sector = NULL,
01012         const csVector3& pos = csVector3(0, 0, 0));
01014   virtual iMeshWrapper* CreateMeshWrapper (iMeshObject*,
01015         const char* name, iSector* sector = NULL,
01016         const csVector3& pos = csVector3(0, 0, 0));
01018   virtual iMeshWrapper* CreateMeshWrapper (const char* name);
01020   virtual iMeshWrapper* LoadMeshWrapper (
01021         const char* name, const char* loaderClassId,
01022         iDataBuffer* input, iSector* sector, const csVector3& pos);
01023 
01024   virtual iClipper2D* GetTopLevelClipper () const;
01025 
01027   virtual void SetAmbientLight (const csColor &c);
01029   virtual void GetAmbientLight (csColor &c) const;
01030 
01031   virtual iMaterialWrapper* FindMaterial (const char* name,
01032         iRegion* region = NULL);
01033   virtual iTextureWrapper* FindTexture (const char* name,
01034         iRegion* region = NULL);
01035   virtual iSector* FindSector (const char* name,
01036         iRegion* region = NULL);
01037   virtual iMeshWrapper* FindMeshObject (const char* name,
01038         iRegion* region = NULL);
01039   virtual iMeshFactoryWrapper* FindMeshFactory (const char* name,
01040         iRegion* region = NULL);
01041   virtual iCameraPosition* FindCameraPosition (const char* name,
01042         iRegion* region = NULL);
01043   virtual iCollection* FindCollection (const char* name,
01044         iRegion* region = NULL);
01045 
01046   //----------------------- iCacheManager implementation ---------------------
01047 
01048   virtual void SetCacheManager (iCacheManager* cache_mgr);
01049   virtual iCacheManager* GetCacheManager ();
01050 
01051   //--------------------- iConfig interface implementation -------------------
01052 
01053   csEngineConfig scfiConfig;
01054 
01055   //----------------Begin-Multi-Context-Support-------------------------------
01056 
01058   virtual void SetContext (iGraphics3D* g3d);
01060   virtual iGraphics3D *GetContext () const;
01061 
01062 private:
01064   void Resize ();
01066   bool resize;
01072   class csEngineState
01073   {
01074   public:
01075     csEngine *engine;
01076     bool resize;
01077     iGraphics2D *G2D;
01078     iGraphics3D *G3D;
01079     csCBuffer* c_buffer;
01080     csCBufferCube* cbufcube;
01082     csEngineState (csEngine *this_engine);
01083 
01085     virtual ~csEngineState ();
01086 
01088     void Activate ();
01089   };
01090 
01091   friend class csEngineState;
01092 
01093   class csEngineStateVector : public csVector
01094   {
01095   public:
01096      // Constructor
01097     csEngineStateVector () : csVector (8, 8) {}
01098     // Destructor
01099     virtual ~csEngineStateVector () { DeleteAll (); }
01100     // Free an item from array
01101     virtual bool FreeItem (csSome Item)
01102     { delete (csEngineState *)Item; return true; }
01103     // Find a state by referenced g2d
01104     virtual int CompareKey (csSome Item, csConstSome Key, int /*Mode*/) const
01105     { return ((csEngineState *)Item)->G3D == (iGraphics3D *)Key ? 0 : -1; }
01106     // Get engine state according to index
01107     inline csEngineState *Get (int n) const
01108     { return (csEngineState *)csVector::Get (n); }
01109 
01110     // Mark engine state to be resized
01111     void Resize (iGraphics2D *g2d);
01112 
01113     // Dispose of engine state dependent on g2d
01114     void Close (iGraphics2D *g2d);
01115   };
01116 
01117   csEngineStateVector *engine_states;
01118 
01119   //------------End-Multi-Context-Support-------------------------------------
01120 
01124   class iObjectInterface : public csObject
01125   {
01126     SCF_DECLARE_EMBEDDED_IBASE (csEngine);
01127   } scfiObject;
01128 };
01129 
01130 #endif // __CS_ENGINE_H__

Generated for Crystal Space by doxygen 1.2.5 written by Dimitri van Heesch, ©1997-2000