Geometry.h
Go to the documentation of this file.
1 #ifndef B_GEOMETRY_H
2 #define B_GEOMETRY_H
3 
4 #include <string>
5 #include <fstream>
6 #include <memory>
7 #include "GeometryData.h"
8 #include "Material.h"
9 #include "Texture.h"
10 #include "Properties.h"
11 #include "Renderer_GL.h"
12 #include "IDrawable.h"
13 #include "Geometry.h"
14 #include "vmmlib/aabb.hpp"
15 
19 class Geometry : public IDrawable
20 {
21 public:
22  /* Typedefs */
23  typedef std::shared_ptr< Vertex > VertexDataPtr;
24  typedef std::shared_ptr< GLushort > IndexDataPtr;
25  typedef std::unordered_map< std::string, PropertiesPtr > PropertiesMap;
26 
27  /* Functions */
28 
31  Geometry(){ _initialized = false; }
32 
35  virtual ~Geometry() {
37  }
38 
42  virtual void initialize(GeometryDataPtr geometryData);
43 
47  virtual void draw(GLenum mode = GL_TRIANGLES) override;
48 
53  virtual void drawInstance(const std::string &instanceName, GLenum mode = GL_TRIANGLES) override;
54 
58  PropertiesPtr addInstance(const std::string &instanceName);
59 
64  virtual void addInstance(const std::string &instanceName, PropertiesPtr instanceProperties);
65 
69  virtual PropertiesPtr getInstanceProperties(const std::string &instanceName);
70 
74  virtual void removeInstance(const std::string &instanceName);
75 
78  virtual void clearInstances();
79 
82  VertexDataPtr getVertexData() { return _vertexData; }
83 
86  IndexDataPtr getIndexData() { return _indexData; }
87 
90  size_t getNumVertices() { return _nVertices; }
91 
94  size_t getNumIndices() { return _nIndices; }
95 
99  void setVertexData(VertexDataPtr arg) { _vertexData = arg; }
100 
104  void setIndexData(IndexDataPtr arg) { _indexData = arg; }
105 
108  MaterialPtr getMaterial() { return _material; }
109 
113  void setMaterial(MaterialPtr arg) { _material = arg; }
114 
117  PropertiesPtr getProperties() { return _properties; }
118 
122  void setProperties(PropertiesPtr arg) { _properties = arg; }
123 
126  vmml::AABBf &getBoundingBoxObjectSpace() { return _boundingBox; }
127 
131  void setBoundingBoxObjectSpace(vmml::AABBf arg) { _boundingBox = arg; }
132 
135  virtual void deleteGeometry()
136  {
137  if (_vertexBuffer)
138  glDeleteBuffers(1, &_vertexBuffer);
139  _initialized = false;
140  }
141 
142 protected:
143 
144  /* Functions */
145 
148  virtual void initializeVertexBuffer();
149 
152  virtual VertexDataPtr allocVertexData(size_t nVertices);
153 
156  virtual IndexDataPtr allocIndexData(size_t nIndices);
157 
161  virtual VertexDataPtr copyVertexData(const GeometryData::VboVertices &arg);
162 
166  virtual IndexDataPtr copyIndexData(const GeometryData::VboIndices &arg);
167 
171  virtual vmml::AABBf createBoundingBoxObjectSpace(const GeometryData::VboVertices &arg);
172 
173 private:
174 
175  /* Variables */
176 
177  bool _initialized = false;
178  GLuint _indexBuffer = 0, _vertexBuffer = 0;
179  size_t _nIndices = 0, _nVertices = 0;
180 
181  VertexDataPtr _vertexData = nullptr;
182  IndexDataPtr _indexData = nullptr;
183 
184  MaterialPtr _material = nullptr;
185  PropertiesPtr _properties = nullptr;
186 
187  vmml::AABBf _boundingBox;
188 
189  PropertiesMap _instances;
190 };
191 
192 typedef std::shared_ptr<Geometry> GeometryPtr;
193 
194 #endif /* defined(B_GEOMETRY_H) */
virtual void initializeVertexBuffer()
Initializes a vertex buffer for the geometry data.
Definition: Geometry.cpp:121
std::vector< GLushort > VboIndices
Definition: GeometryData.h:128
IndexDataPtr getIndexData()
Returns a pointer to the indices of the geometry.
Definition: Geometry.h:86
std::shared_ptr< Properties > PropertiesPtr
Definition: Properties.h:179
virtual VertexDataPtr copyVertexData(const GeometryData::VboVertices &arg)
Copies the given vertices into the geometry object.
Definition: Geometry.cpp:103
void setMaterial(MaterialPtr arg)
Sets the material of the geometry.
Definition: Geometry.h:113
void setVertexData(VertexDataPtr arg)
Sets the vertices of the geometry.
Definition: Geometry.h:99
void setProperties(PropertiesPtr arg)
Sets the properties of the geometry.
Definition: Geometry.h:122
Geometry()
Constructor.
Definition: Geometry.h:31
std::vector< Vertex > VboVertices
Definition: GeometryData.h:127
virtual IndexDataPtr copyIndexData(const GeometryData::VboIndices &arg)
Copies the given indices into the geometry object.
Definition: Geometry.cpp:112
void setIndexData(IndexDataPtr arg)
Sets the indices of the geometry.
Definition: Geometry.h:104
PropertiesPtr addInstance(const std::string &instanceName)
Creates an instance of this geometry.
Definition: Geometry.cpp:55
virtual PropertiesPtr getInstanceProperties(const std::string &instanceName)
Get the properties of a geometry instance.
Definition: Geometry.cpp:69
virtual void draw(GLenum mode=GL_TRIANGLES) override
Draws the geometry to the screen.
Definition: Geometry.cpp:18
An interface for drawable objects.
Definition: IDrawable.h:9
virtual void removeInstance(const std::string &instanceName)
Removes an instance.
Definition: Geometry.cpp:75
MaterialPtr getMaterial()
Returns a pointer to the material of the geometry.
Definition: Geometry.h:108
A geometry object containing vertices and indices that can be rendered to the screen.
Definition: Geometry.h:19
virtual ~Geometry()
Virtual destructor.
Definition: Geometry.h:35
std::shared_ptr< GeometryData > GeometryDataPtr
Definition: GeometryData.h:138
virtual VertexDataPtr allocVertexData(size_t nVertices)
Allocates the vertex data.
Definition: Geometry.cpp:87
std::unordered_map< std::string, PropertiesPtr > PropertiesMap
Definition: Geometry.h:25
virtual void initialize(GeometryDataPtr geometryData)
Initializes the geometry object based on geometry data.
Definition: Geometry.cpp:5
virtual void drawInstance(const std::string &instanceName, GLenum mode=GL_TRIANGLES) override
Draws an instance of the geometry to the screen.
Definition: Geometry.cpp:34
virtual IndexDataPtr allocIndexData(size_t nIndices)
Allocates the index data.
Definition: Geometry.cpp:95
VertexDataPtr getVertexData()
Returns a pointer to the vertices of the geometry.
Definition: Geometry.h:82
size_t getNumIndices()
Returns the number of indices in the geometry.
Definition: Geometry.h:94
virtual vmml::AABBf createBoundingBoxObjectSpace(const GeometryData::VboVertices &arg)
Creates an axis-aligned bounding box around the object.
Definition: Geometry.cpp:130
virtual void deleteGeometry()
Deletes the geometry.
Definition: Geometry.h:135
std::shared_ptr< GLushort > IndexDataPtr
Definition: Geometry.h:24
vmml::AABBf & getBoundingBoxObjectSpace()
Returns the bounding box of the geometry in object space.
Definition: Geometry.h:126
void setBoundingBoxObjectSpace(vmml::AABBf arg)
Sets the bounding box of the geometry in object space.
Definition: Geometry.h:131
size_t getNumVertices()
Returns the number of vertices in the geometry.
Definition: Geometry.h:90
virtual void clearInstances()
Removes all instances.
Definition: Geometry.cpp:80
PropertiesPtr getProperties()
Returns a pointer to the properties of the geometry.
Definition: Geometry.h:117
std::shared_ptr< Material > MaterialPtr
Definition: Material.h:135
std::shared_ptr< Geometry > GeometryPtr
Definition: Geometry.h:192
std::shared_ptr< Vertex > VertexDataPtr
Definition: Geometry.h:23