Model.h
Go to the documentation of this file.
1 #ifndef B_MODEL_H
2 #define B_MODEL_H
3 
4 #include <memory>
5 #include <string>
6 #include <unordered_map>
7 
8 #include "IDrawable.h"
9 #include "Geometry.h"
10 #include "Material.h"
11 #include "Renderer_GL.h"
12 
13 class ObjectManager;
14 class ModelData;
15 
19 class Model : public IDrawable
20 {
21 public:
22  /* Typedefs */
23  typedef std::unordered_map< std::string, GeometryPtr > GroupMap;
24  typedef std::shared_ptr< std::unordered_map< ShaderPtr, PropertiesPtr > > InstanceMapPtr;
25  typedef std::unordered_map< std::string, InstanceMapPtr > InstancesMap;
26 
27  /* Functions */
28 
31  Model() {}
32 
42  Model(ObjectManager *o, const ModelData &modelData, GLuint shaderMaxLights, bool variableNumberOfLights, bool shaderFromFile, bool ambientLighting, PropertiesPtr properties = nullptr);
43 
50  Model(ObjectManager *o, const ModelData &modelData, ShaderPtr shader, PropertiesPtr properties = nullptr);
51 
57  Model(const ModelData &modelData, MaterialPtr material, PropertiesPtr properties = nullptr);
58 
61  virtual ~Model() { deleteModelGeometry(); }
62 
66  virtual void draw(GLenum mode = GL_TRIANGLES) override;
67 
72  virtual void draw(const std::string &geometryName, GLenum mode = GL_TRIANGLES);
73 
78  virtual void drawInstance(const std::string &instanceName, GLenum mode = GL_TRIANGLES) override;
79 
86  virtual InstanceMapPtr addInstance(const std::string &instanceName);
87 
92  virtual PropertiesPtr getInstanceProperties(const std::string &instanceName, const std::string &geometryName);
93 
97  virtual InstanceMapPtr getInstanceProperties(const std::string &instanceName);
98 
102  virtual void removeInstance(const std::string &instanceName);
103 
106  virtual void clearInstances();
107 
110  MaterialPtr getMaterial() { return _material; }
111 
116  {
117  _material = arg;
118 
119  for (auto i = _groups.begin(); i != _groups.end(); ++i)
120  {
121  i->second->setMaterial(_material);
122  }
123  }
124 
127  PropertiesPtr getProperties() { return _properties; }
128 
133  {
134  _properties = arg;
135 
136  for (auto i = _groups.begin(); i != _groups.end(); ++i)
137  {
138  i->second->setProperties(_properties);
139  }
140  }
141 
144  GroupMap &getGroups() { return _groups; }
145 
148  vmml::AABBf &getBoundingBoxObjectSpace() { return _boundingBox; }
149 
153  void setBoundingBoxObjectSpace(vmml::AABBf arg) { _boundingBox = arg; }
154 
159  virtual void addGeometry(const std::string &name, GeometryPtr geometry);
160 
164  virtual void removeGeometry(const std::string &name) { _groups.erase(name); }
165 
168  virtual void deleteModelGeometry()
169  {
170  for (auto i = _groups.begin(); i != _groups.end(); ++i)
171  {
172  i->second->deleteGeometry();
173  }
174  _groups.clear();
175  }
176 
177 private:
178 
179  /* Variables */
180 
181  GroupMap _groups;
182  MaterialPtr _material = nullptr;
183  PropertiesPtr _properties = nullptr;
184  vmml::AABBf _boundingBox;
185  InstancesMap _instances;
186 };
187 
188 typedef std::shared_ptr<Model> ModelPtr;
189 
190 #endif /* defined(B_MODEL_H) */
GroupMap & getGroups()
Returns the groups of geometry of the model.
Definition: Model.h:144
virtual void draw(GLenum mode=GL_TRIANGLES) override
Draws the model to the screen.
Definition: Model.cpp:65
PropertiesPtr getProperties()
Returns the properties of the model.
Definition: Model.h:127
A 3d model that can be rendered to the screen.
Definition: Model.h:19
void setMaterial(MaterialPtr arg)
Sets the material of the model.
Definition: Model.h:115
virtual void removeGeometry(const std::string &name)
Removes geometry from the model.
Definition: Model.h:164
std::shared_ptr< Shader > ShaderPtr
Definition: Shader.h:211
std::shared_ptr< Properties > PropertiesPtr
Definition: Properties.h:179
virtual void clearInstances()
Removes all instances of the model and associated geometry.
Definition: Model.cpp:133
vmml::AABBf & getBoundingBoxObjectSpace()
Returns the bounding box of the geometry in object space.
Definition: Model.h:148
virtual ~Model()
Virtual destructor.
Definition: Model.h:61
virtual PropertiesPtr getInstanceProperties(const std::string &instanceName, const std::string &geometryName)
Get the properties of a geometry instance.
Definition: Model.cpp:110
std::unordered_map< std::string, GeometryPtr > GroupMap
Definition: Model.h:23
virtual void deleteModelGeometry()
Deletes all geometry of the model.
Definition: Model.h:168
An interface for drawable objects.
Definition: IDrawable.h:9
void setBoundingBoxObjectSpace(vmml::AABBf arg)
Sets the bounding box of the geometry in object space.
Definition: Model.h:153
virtual void removeInstance(const std::string &instanceName)
Removes an instance of the model and associated geometry.
Definition: Model.cpp:122
virtual void addGeometry(const std::string &name, GeometryPtr geometry)
Adds geometry to the model.
Definition: Model.cpp:144
virtual void drawInstance(const std::string &instanceName, GLenum mode=GL_TRIANGLES) override
Draws an instance of the model to the screen.
Definition: Model.cpp:78
Model()
Constructor.
Definition: Model.h:31
virtual InstanceMapPtr addInstance(const std::string &instanceName)
Creates an instance of this model and associated geometry.
Definition: Model.cpp:86
void setProperties(PropertiesPtr arg)
Sets the properties of the model.
Definition: Model.h:132
std::shared_ptr< std::unordered_map< ShaderPtr, PropertiesPtr > > InstanceMapPtr
Definition: Model.h:24
This class manages all objects in a project and makes sure no object is created twice.
Definition: ObjectManager.h:32
std::unordered_map< std::string, InstanceMapPtr > InstancesMap
Definition: Model.h:25
Definition: ModelData.h:11
MaterialPtr getMaterial()
Returns the material of the model.
Definition: Model.h:110
std::shared_ptr< Model > ModelPtr
Definition: Model.h:188
std::shared_ptr< Material > MaterialPtr
Definition: Material.h:135
std::shared_ptr< Geometry > GeometryPtr
Definition: Geometry.h:192