Cafu Engine
CollisionModelMan.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_CLIPSYS_COLLISION_MODEL_MANAGER_HPP_INCLUDED
8 #define CAFU_CLIPSYS_COLLISION_MODEL_MANAGER_HPP_INCLUDED
9 
10 #include <string>
11 
12 #include "Math3D/Vector3.hpp"
13 #include "Math3D/BoundingBox.hpp"
14 
15 
16 class MaterialT;
17 
18 
19 namespace cf
20 {
21  namespace SceneGraph { namespace aux { class PoolT; } }
22 
23 
24  namespace ClipSys
25  {
26  class CollisionModelT;
27 
28 
29  /// This class provides (an interface to) the creation, management and destruction of collision models.
30  /// It employs reference counting for collision models that are loaded from file.
31  /// The interface is specified as an ABC in order to be able to share it across exe/dll boundaries.
32  ///
33  /// The important feature is that it allows the flexible, care-free handling of collision models:
34  /// no matter where a model "comes from" (loaded file, file stream, bounding box, ...), it is created
35  /// and deleted in a uniform manner, which in turn is an important feature for the game (entity) code.
36  /// The const-ness of the returned pointers is obviously a consequence of the instance-sharing implied
37  /// by the reference counting: You can inspect, but not mutate/modify shared objects.
39  {
40  public:
41 
42  /// The virtual destructor.
43  virtual ~CollModelManI() { }
44 
45  /// Loads a collision model from the file specified by FileName.
46  /// If the collision model has been loaded before, reference counting is employed.
47  /// @param FileName The name of the file to load the collision model from.
48  /// @returns (a pointer to) the collision model instance that has been loaded from the given file, or NULL on failure.
49  virtual const CollisionModelT* GetCM(const std::string& FileName) = 0;
50 
51  // /// Loads a collision model from the specified instream.
52  // /// @param InFile The instream to load the collision model from.
53  // /// @param Pool Pool of vectors and strings used multiple times in InFile.
54  // /// @returns the loaded collision model instance.
55  // /// TODO: Es wäre SUPER wenn wir in InFile irgendwie einen unique Name o. Hash speichern könnten!!!!
56  // /// (Denn Sv und Cl laden ja unabh. voneinander dasselbe .cw world file. Ein gemeinsamer Hash würde eine einzige Instanz sharen.)
57  // virtual const CollisionModelT* GetCM(std::istream& InFile, SceneGraph::aux::PoolT& Pool, const ArrayT<CollisionModelStaticT::TerrainRefT>& Terrains)=0;
58 
59  /// Creates a collision model from the given explicit mesh.
60  ///
61  /// @param Width The width of the mesh.
62  /// @param Height The height of the mesh.
63  /// @param Mesh The vertices of the mesh.
64  /// @param Material The material reported in collision results when a trace hit.
65  /// @param MIN_NODE_SIZE The minimum size (side length) that a node should not fall below.
66  ///
67  /// @returns the matching collision model instance.
68  virtual const CollisionModelT* GetCM(unsigned long Width, unsigned long Height, const ArrayT<Vector3dT>& Mesh, MaterialT* Material, const double MIN_NODE_SIZE) = 0;
69 
70  /// Creates a collision model from the given (axis-aligned) bounding-box.
71  /// @param BB The bounding box to create a collision model for.
72  /// @param Material The material to use for the collision model.
73  /// @returns the desired collision model.
74  virtual const CollisionModelT* GetCM(const BoundingBox3T<double>& BB, MaterialT* Material) = 0;
75 
76  /// Creates another collision model from a given collision model.
77  /// a) If the given collision model has been created with other methods of this CollModelManI earlier,
78  /// the implementation will simply increase the related reference counter and return the same pointer.
79  /// b) If the given collision model has been created "externally" with some other means (e.g. direct instantiation
80  /// of a concrete collision model class), the CollModelManI will create a record about it with a reference count of 1,
81  /// assume that the original instance is not freed/deleted by the caller as long as it has a non-zero reference count,
82  /// and will not attempt to delete the instance when the count finally drops to 0.
83  /// @param CollisionModel The collision model to create another collision model from.
84  /// @returns a matching collision model instance.
85  virtual const CollisionModelT* GetCM(const CollisionModelT* CollisionModel) = 0;
86 
87 
88  /// Returns the file name the given collision model has been loaded from (using the GetCM(const std::string& FileName) method).
89  /// @param CollisionModel The collision model for which return the associated file name.
90  /// @returns the file name of the collision model, or "" (the empty string) if the model was created by another method.
91  virtual const std::string& GetFileName(const CollisionModelT* CollisionModel) const = 0;
92 
93  /// Frees the given collision model (taking reference counting into account if necessary).
94  /// @param CollisionModel The collision model to be freed.
95  virtual void FreeCM(const CollisionModelT* CollisionModel) = 0;
96 
97  /// Returns the number of unique, physical collision model instances managed by this class.
98  virtual unsigned long GetUniqueCMCount() const = 0;
99  };
100 
101 
102  /// A global pointer to an implementation of the CollModelManI interface.
103  ///
104  /// Each module (exe or dll) that uses this pointer must somewhere provide exactly one definition for it (none is provided by the ClipSys library).
105  /// That is, typically the main.cpp or similar file of each exe and dll must contain a line like
106  /// cf::ClipSys::CollModelManI* cf::FileSys::CollModelMan=NULL;
107  /// or else the module will not link successfully due to an undefined symbol.
108  ///
109  /// Exe files will then want to reset this pointer to an instance of a CollModelManImplT during their initialization
110  /// e.g. by code like: cf::ClipSys::CollModelMan=new cf::ClipSys::CollModelManImplT;
111  /// Note that the CollModelManImplT ctor may require that other interfaces (e.g. the Console) have been inited first.
112  ///
113  /// Dlls typically get one of their init functions called immediately after they have been loaded.
114  /// By doing so, the exe passes a pointer to its above instance to the dll, which in turn copies it to its CollModelMan variable.
115  extern CollModelManI* CollModelMan;
116  }
117 }
118 
119 #endif
virtual unsigned long GetUniqueCMCount() const =0
Returns the number of unique, physical collision model instances managed by this class.
This class provides (an interface to) the creation, management and destruction of collision models...
Definition: CollisionModelMan.hpp:38
virtual void FreeCM(const CollisionModelT *CollisionModel)=0
Frees the given collision model (taking reference counting into account if necessary).
This class represents a surface material ("A datastructural representation of a scripts material def...
Definition: Material.hpp:22
virtual const CollisionModelT * GetCM(const std::string &FileName)=0
Loads a collision model from the file specified by FileName.
virtual const std::string & GetFileName(const CollisionModelT *CollisionModel) const =0
Returns the file name the given collision model has been loaded from (using the GetCM(const std::stri...
This is the base class for collision models, defining their common interface.
Definition: CollisionModel_base.hpp:29
virtual ~CollModelManI()
The virtual destructor.
Definition: CollisionModelMan.hpp:43
Definition: Renderer.hpp:16