Cafu Engine
Loader.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_MODEL_LOADER_HPP_INCLUDED
8 #define CAFU_MODEL_LOADER_HPP_INCLUDED
9 
10 #include "Model_cmdl.hpp"
11 
12 
13 /// The base class for importing arbitrary model files into Cafu models.
14 /// The concrete derived classes determine which specific file format is imported.
16 {
17  public:
18 
19  class UserCallbacksI;
20  class LoadErrorT;
21 
22  enum FlagsT
23  {
24  NONE =0x00,
25  REMOVE_DEGEN_TRIANGLES=0x01,
26  REMOVE_UNUSED_VERTICES=0x02,
27  REMOVE_UNUSED_WEIGHTS =0x04
28  };
29 
30 
31  /// The constructor.
32  ModelLoaderT(const std::string& FileName, int Flags);
33 
34  /// The virtual destructor.
35  virtual ~ModelLoaderT() { }
36 
37  /// Returns the file name of the imported model.
38  /// This method is reimplemented in the \c LoaderDlodT class.
39  virtual const std::string& GetFileName() const { return m_FileName; }
40 
41  /// Actually loads the file data into the appropriate parts of the Cafu model.
43 
44  /// Loads the skins of the Cafu model.
45  virtual void Load(ArrayT<CafuModelT::SkinT>& Skins, const MaterialManagerImplT& MaterialMan)=0;
46 
47  /// Loads the GUI fixtures of the Cafu model.
48  virtual void Load(ArrayT<CafuModelT::GuiFixtureT>& GuiFixtures)=0;
49 
50  /// Loads the animation channels (groups of joints) of the Cafu model.
51  virtual void Load(ArrayT<CafuModelT::ChannelT>& Channels)=0;
52 
53  /// Loads the dlod-model and dlod-distance at the given level.
54  virtual bool Load(unsigned int Level, CafuModelT*& DlodModel, float& DlodDist)=0;
55 
56  /// Postprocesses the file data according to flags given to the constructor.
57  virtual void Postprocess(ArrayT<CafuModelT::MeshT>& Meshes);
58 
59 
60  protected:
61 
62  /// Removes triangles with zero-length edges from the given mesh.
63  /// This is especially important because such triangles "connect" two vertices that the CafuModelT code
64  /// considers as "geometrical duplicates" of each other. That is, a single triangle refers to the same
65  /// vertex coordinate twice, which triggers related assertions in debug builds.
67 
68  /// Removes unused vertices from the given mesh.
70 
71  /// Makes sure that vertices that are geo-dups of each other refer to the same set of weights.
73 
74  /// Removes unused weights from the given mesh (should be called after RemoveUnusedVertices()).
76 
77  /// Creates and returns a fail-safe wire-frame material with the given name,
78  /// for use when a material with more detailed or more specific settings is not available.
79  MaterialT CreateDefaultMaterial(const std::string& MatName, bool EditorSave=true) const;
80 
81  const std::string m_FileName;
82  const int m_Flags;
83 };
84 
85 
86 /// An interface for user callbacks.
87 /// A concrete model loader may require e.g. asking the user for a password to open the file.
88 /// The calling code can implement this interface and pass it to the concrete loader in order to achieve the desired functionality.
90 {
91  public:
92 
93  /// Asks the user for a password to open the model file.
94  /// @returns The entered password, or the empty string for cancel/none.
95  virtual std::string GetPasswordFromUser(const std::string& Message, const std::string& Caption="Enter password")=0;
96 
97  // /// Returns an output stream for any log messages that the loader wants to present to the user.
98  // std::ostream& GetLog()=0;
99 
100  // /// Tells the implementation how much of the model has already been loaded.
101  // void SetProgress(float Percent)=0;
102 };
103 
104 
105 /// A class that is thrown on model load errors.
106 class ModelLoaderT::LoadErrorT : public std::runtime_error
107 {
108  public:
109 
110  LoadErrorT(const std::string& Message);
111 };
112 
113 
114 /// The base class for importing additional animations into an existing CafuModelT.
116 {
117  public:
118 
119  /// The constructor.
120  AnimImporterT(const std::string& FileName);
121 
122  /// Returns the name of the file the animations are imported from.
123  virtual const std::string& GetFileName() const { return m_FileName; }
124 
125  /// Imports and returns the animation sequences from the file, optionally referring to the joints and meshes of the related model.
126  /// It is up to the caller to actually add the imported sequences to the related model instance.
128 
129 
130  protected:
131 
132  const std::string m_FileName;
133 };
134 
135 #endif
This class represents a native Cafu model.
Definition: Model_cmdl.hpp:45
The base class for importing additional animations into an existing CafuModelT.
Definition: Loader.hpp:115
void RemoveDegenTriangles(CafuModelT::MeshT &Mesh)
Removes triangles with zero-length edges from the given mesh.
Definition: Loader.cpp:37
void RemoveUnusedWeights(CafuModelT::MeshT &Mesh)
Removes unused weights from the given mesh (should be called after RemoveUnusedVertices()).
Definition: Loader.cpp:97
The base class for importing arbitrary model files into Cafu models.
Definition: Loader.hpp:15
ModelLoaderT(const std::string &FileName, int Flags)
The constructor.
Definition: Loader.cpp:18
An interface for user callbacks.
Definition: Loader.hpp:89
This struct defines a triangle mesh in the model.
Definition: Model_cmdl.hpp:63
This class represents a surface material ("A datastructural representation of a scripts material def...
Definition: Material.hpp:22
virtual ArrayT< CafuModelT::AnimT > Import(const ArrayT< CafuModelT::JointT > &Joints, const ArrayT< CafuModelT::MeshT > &Meshes)=0
Imports and returns the animation sequences from the file, optionally referring to the joints and mes...
virtual std::string GetPasswordFromUser(const std::string &Message, const std::string &Caption="Enter password")=0
Asks the user for a password to open the model file.
AnimImporterT(const std::string &FileName)
The constructor.
Definition: Loader.cpp:148
This class implements the MaterialManagerI interface.
Definition: MaterialManagerImpl.hpp:23
virtual void Postprocess(ArrayT< CafuModelT::MeshT > &Meshes)
Postprocesses the file data according to flags given to the constructor.
Definition: Loader.cpp:25
virtual ~ModelLoaderT()
The virtual destructor.
Definition: Loader.hpp:35
A class that is thrown on model load errors.
Definition: Loader.hpp:106
virtual void Load(ArrayT< CafuModelT::JointT > &Joints, ArrayT< CafuModelT::MeshT > &Meshes, ArrayT< CafuModelT::AnimT > &Anims, MaterialManagerImplT &MaterialMan)=0
Actually loads the file data into the appropriate parts of the Cafu model.
virtual const std::string & GetFileName() const
Returns the file name of the imported model.
Definition: Loader.hpp:39
virtual const std::string & GetFileName() const
Returns the name of the file the animations are imported from.
Definition: Loader.hpp:123
void AbandonDuplicateWeights(CafuModelT::MeshT &Mesh)
Makes sure that vertices that are geo-dups of each other refer to the same set of weights...
Definition: Loader.cpp:86
void RemoveUnusedVertices(CafuModelT::MeshT &Mesh)
Removes unused vertices from the given mesh.
Definition: Loader.cpp:56
MaterialT CreateDefaultMaterial(const std::string &MatName, bool EditorSave=true) const
Creates and returns a fail-safe wire-frame material with the given name, for use when a material with...
Definition: Loader.cpp:129