Spinning Topp Logo BlackTopp Studios
inc
trianglebuffer.h
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 /*
41  -----------------------------------------------------------------------------
42  This source file is part of ogre-procedural
43 
44  For the latest info, see http://code.google.com/p/ogre-procedural/
45 
46  Copyright (c) 2010-2013 Michael Broutin
47 
48  Permission is hereby granted, free of charge, to any person obtaining a copy
49  of this software and associated documentation files (the "Software"), to deal
50  in the Software without restriction, including without limitation the rights
51  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
52  copies of the Software, and to permit persons to whom the Software is
53  furnished to do so, subject to the following conditions:
54 
55  The above copyright notice and this permission notice shall be included in
56  all copies or substantial portions of the Software.
57 
58  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
59  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
60  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
61  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
62  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
63  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
64  THE SOFTWARE.
65  -----------------------------------------------------------------------------
66  */
67 #ifndef _graphicsproceduraltrianglebuffer_h
68 #define _graphicsproceduraltrianglebuffer_h
69 
70 #include "Graphics/graphicsenumerations.h"
71 #include "Graphics/Procedural/proceduraldatatypes.h"
72 #include "matrix4x4.h"
73 
74 namespace Mezzanine
75 {
76  namespace Graphics
77  {
78  class Mesh;
79  namespace Procedural
80  {
81  class TriangleBuffer;
82  ///////////////////////////////////////////////////////////////////////////////
83  /// @brief A simple definition for a Vertex to be used when procedurally generating meshes.
84  /// @details
85  ///////////////////////////////////////
87  {
88  /// @brief The position of the vertex in local space.
90  /// @brief The facing direction of the vertex in local space.
92  /// @brief The UV coordinate for applying textures.
94 
95  /// @brief Blank constructor.
97  { }
98  /// @brief Detailed constructor.
99  /// @param Pos The position of the vertex in local space.
100  /// @param Norm The facing direction of the vertex in local space.
101  /// @param TexCoord The UV coordinate for applying textures.
102  Vertex(const Vector3& Pos, const Vector3& Norm, const Vector2& TexCoord) :
103  Position(Pos),
104  Normal(Norm),
105  UV(TexCoord)
106  { }
107  /// @brief Class destructor.
109  { }
110  };//Vertex
111 
112  /// @brief Basic container type for Vertex storage.
113  typedef std::vector<Vertex> VertexContainer;
114  /// @brief Iterator type for Vertex instances in a VertexContainer.
115  typedef VertexContainer::iterator VertexIterator;
116  /// @brief Const Iterator type for Vertex instances in a VertexContainer.
117  typedef VertexContainer::const_iterator ConstVertexIterator;
118 
119  ///////////////////////////////////////////////////////////////////////////////
120  /// @brief A convenience class used to describe a small portion of the mesh/buffer.
121  /// @details
122  ///////////////////////////////////////
124  {
125  ///////////////////////////////////////////////////////////////////////////////
126  // Public Data Members
127 
128  /// @brief The start vertex of this section in the parent buffer.
130  /// @brief The end vertex of this section in the parent buffer.
132  /// @brief The start indicy of this section in the parent buffer.
134  /// @brief The end indicy of this section in the parent buffer.
136  /// @brief The render operation to be used for the section. See Graphics::RenderOperation enum for more info.
138  /// @brief The name of the material to be used with this section.
140  /// @brief The asset group where the material can be found.
142 
143  ///////////////////////////////////////////////////////////////////////////////
144  // Construction and Destruction
145 
146  /// @brief Class constructor.
148  FirstVertex(0),
149  LastVertex(0),
150  FirstIndex(0),
151  LastIndex(0),
152  RenderOp(Graphics::RO_TriangleList)
153  { }
154  /// @brief Class destructor.
156  { }
157 
158  ///////////////////////////////////////////////////////////////////////////////
159  // Utility
160 
161  /// @brief Clears all the data on this BufferSection back to a blank initialization.
162  void Clear()
163  {
164  this->FirstVertex = 0;
165  this->LastVertex = 0;
166  this->FirstIndex = 0;
167  this->LastIndex = 0;
168  this->RenderOp = Graphics::RO_TriangleList;
169  this->MaterialName.clear();
170  this->MaterialGroup.clear();
171  }
172 
173  ///////////////////////////////////////////////////////////////////////////////
174  // Operators
175 
176  /// @brief Assignment operator.
177  /// @param Other The other BufferSection to be copied to this.
178  void operator=(const BufferSection& Other)
179  {
180  this->FirstVertex = Other.FirstVertex;
181  this->LastVertex = Other.LastVertex;
182  this->FirstIndex = Other.FirstIndex;
183  this->LastIndex = Other.LastIndex;
184  this->RenderOp = Other.RenderOp;
185  this->MaterialName = Other.MaterialName;
186  this->MaterialGroup = Other.MaterialGroup;
187  }
188  };//BufferSection
189 
190  ///////////////////////////////////////////////////////////////////////////////
191  /// @brief A convenience buffer that stores vertices and indices of a mesh to be generated.
192  /// @details This buffer is different from native Graphics buffers in that it both has a combined API for the
193  /// manipulation of vertices and indices and it doesn't create a cooresponding buffer in video card memory.
194  /// This buffer exists solely in system memory and nothing is allocated on the video card until a mesh is
195  /// generated.
196  ///////////////////////////////////////
198  {
199  public:
200  /// @brief Basic container type for Section storage in this class.
201  typedef std::vector<BufferSection> SectionContainer;
202  /// @brief Iterator type for Section instances stored in this class.
203  typedef SectionContainer::iterator SectionIterator;
204  /// @brief Const Iterator type for Section instances stored in this class.
205  typedef SectionContainer::const_iterator ConstSectionIterator;
206  protected:
207  /// @internal
208  /// @brief Container storing all of the sections of this buffer.
209  SectionContainer Sections;
210  /// @internal
211  /// @brief Container storing all of the Vertices in this buffer.
212  VertexContainer Vertices;
213  /// @internal
214  /// @brief Container storing all of the Indices in this buffer.
216  /// @internal
217  /// @brief Temporary storage for the current section being worked on.
219  /// @internal
220  /// @brief Convenience pointer to the Vertex currently being manipulated.
222  /// @internal
223  /// @brief The user estimated Vertex count for this buffer.
225  /// @internal
226  /// @brief The user estimated Index count for this buffer.
228  /// @internal
229  /// @brief Offset used to set where buffer/mesh appends begin.
231  public:
232  /// @brief Class constructor.
233  TriangleBuffer();
234  /// @brief Class destructor.
235  ~TriangleBuffer();
236 
237  ///////////////////////////////////////////////////////////////////////////////
238  // Utility
239 
240  /// @brief Appends the contents of another buffer to this buffer.
241  /// @param Other The other TriangleBuffer to append to this.
242  void AppendBuffer(const TriangleBuffer& Other);
243  /// @brief Appends the contents of another buffer to this buffer as a separate section of the Mesh.
244  /// @param Other The other TriangleBuffer to append to this.
245  /// @param MatName The name of the material to be used with this section.
246  /// @param MatGroup The asset group where the Material can be found.
247  /// @param RenderOp The render operation describing how the Vertices should be treated for assembling the Mesh. See Graphics::RenderOperation enum for more information.
248  void AppendBufferAsSection(const TriangleBuffer& Other, const String& MatName = "", const String& MatGroup = "", const Whole RenderOp = Graphics::RO_TriangleList);
249  /// @brief Builds a Mesh from this buffer.
250  /// @note The returned mesh will need a Material applied to it for proper rendering.
251  /// @param MeshName The name to give to the generated Mesh.
252  /// @param MeshGroup The asset group to place the mesh in.
253  /// @param MatName The name of the Material to apply to the Mesh. This is only used if the buffer isn't using sections.
254  /// @param MatGroup The asset group where the Material can be found. This is only used if the buffer isn't using sections.
255  /// @return Returns a pointer to the created Mesh.
256  Mesh* GenerateMesh(const String& MeshName, const String& MeshGroup, const String& MatName = "", const String& MatGroup = "") const;
257 
258  /// @brief Gives an estimation of the number of vertices need for this triangle buffer.
259  /// @remarks If this function is called several times, it means an extra vertices count, not an absolute measure.
260  /// @param VertexCount The amount of additional Vertices to allocate for.
261  void EstimateVertexCount(const Whole VertexCount);
262  /// @brief Gives an estimation of the number of indices needed for this triangle buffer.
263  /// @remarks If this function is called several times, it means an extra indices count, not an absolute measure.
264  /// @param IndexCount The amount of additional Indexes to allocate for.
265  void EstimateIndexCount(const Whole IndexCount);
266 
267  /// @brief Rebase index offset.
268  /// @remarks Call this function before you add a new mesh to the triangle buffer.
269  void RebaseOffset();
270 
271  /// @brief Gets a modifiable reference to Sections.
272  /// @return Returns a reference to the container storing the Sections of this buffer.
273  SectionContainer& GetSections();
274  /// @brief Gets a non-modifiable reference to Sections.
275  /// @return Returns a const reference to the container storing the Sections of this buffer.
276  const SectionContainer& GetSections() const;
277 
278  /// @brief Gets a modifiable reference to Vertices.
279  /// @return Returns a reference to the container storing the Vertices of this buffer.
280  VertexContainer& GetVertices();
281  /// @brief Gets a non-modifiable reference to Vertices.
282  /// @return Returns a const reference to the container storing the Vertices of this buffer.
283  const VertexContainer& GetVertices() const;
284  /// @brief Gets a modifiable reference to Indices.
285  /// @return Returns a reference to the container storing the Indices of this buffer.
286  IndexContainer& GetIndices();
287  /// @brief Gets a non-modifiable reference to Indices.
288  /// @return Returns a const reference to the container storing the Indices of this buffer.
289  const IndexContainer& GetIndices() const;
290 
291  ///////////////////////////////////////////////////////////////////////////////
292  // Section Utility
293 
294  /// @brief Gets a BufferSection that can be appended to the end of this buffer.
295  /// @remarks Sections are a useful tool for if and when you want to generate a Mesh with multiple SubMeshes. The usefulness of multiple
296  /// SubMeshes is being able to assign more than one material to a mesh for whatever reason (there are plenty). When you start a Section
297  /// if you have already added some vertex data to the mesh then all data input thus far will become a section and a new, second section
298  /// will be started. Likewise, if a section is in progress and you call BeginSection then the previous section will be ended. \n
299  /// Section names and their materials are optional. However if you do not assign a material to a completed mesh prior
300  /// to rendered it will render as just white (probably not what you are going for). So while setting a material name is optional at this
301  /// stage it is a good idea to get around to.
302  /// @param MatName The name of the material to be used with this section.
303  /// @param MatGroup The asset group where the Material can be found.
304  /// @param RenderOp The render operation describing how the Vertices should be treated for assembling the Mesh. See Graphics::RenderOperation enum for more information.
305  void BeginSection(const String& MatName = "", const String& MatGroup = "", const Whole RenderOp = Graphics::RO_TriangleList);
306  /// @brief Sets a BufferSection as the end section of this buffer.
307  /// @param Sect The BufferSection to set as the end of the buffer.
308  void EndSection();
309  /// @brief Gets whether or not this buffer is divided into sections.
310  /// @return Returns true if this buffer will generate multiple SubMeshes when it makes a Mesh, false otherwise.
311  Boole IsUsingSections() const;
312  /// @brief Gets whether or not a section has been started and not yet closed.
313  /// @return Returns true if this buffer has an open section that is not yet ended, false otherwise.
314  Boole IsWorkingOnSection() const;
315 
316  ///////////////////////////////////////////////////////////////////////////////
317  // Vertex Management
318 
319  /// @brief Adds a premade Vertex to the buffer.
320  /// @param Vert The premade Vertex to be appended to this buffer.
321  /// @return Returns a reference to this.
322  TriangleBuffer& AddVertex(const Vertex& Vert);
323  /// @brief Adds a new vertex to the buffer.
324  /// @param Loc The location to set the new Vertex at, in local space.
325  /// @param Norm The facing direction of the new Vertex, in local space.
326  /// @param UV The texture coordinate to set for the new Vertex.
327  /// @return Returns a reference to this.
328  TriangleBuffer& AddVertex(const Vector3& Loc, const Vector3& Norm, const Vector2& UV);
329  /// @brief Adds a new vertex to the buffer.
330  /// @param Loc The location to set the new Vertex at, in local space.
331  /// @return Returns a reference to this.
332  TriangleBuffer& AddVertex(const Vector3& Loc);
333  /// @brief Adds a new vertex to the buffer.
334  /// @param X The location of the new Vertex on the X axis, in local space.
335  /// @param Y The location of the new Vertex on the Y axis, in local space.
336  /// @param Z The location of the new Vertex on the Z axis, in local space.
337  /// @return Returns a reference to this.
338  TriangleBuffer& AddVertex(const Real X, const Real Y, const Real Z);
339  /// @brief Sets the normal of the current vertex.
340  /// @param Norm The facing direction to set for the current Vertex.
341  /// @return Returns a reference to this.
342  TriangleBuffer& SetNormal(const Vector3& Norm);
343  /// @brief Sets the texture coordinates of the current vertex.
344  /// @param U The U component of texture UV component to set for the current Vertex.
345  /// @param V The V component of texture UV component to set for the current Vertex.
346  /// @return Returns a reference to this.
347  TriangleBuffer& SetTextureCoord(const Real U, const Real V);
348  /// @brief Sets the texture coordinates of the current vertex.
349  /// @param UV The texture coordinate to set for the current Vertex.
350  /// @return Returns a reference to this.
351  TriangleBuffer& SetTextureCoord(const Vector2& UV);
352 
353  /// @brief Applies a matrix to transform all vertices inside this triangle buffer.
354  /// @param Mat A full transform to be applied to each Vertex in this buffer.
355  /// @return Returns a reference to this.
356  TriangleBuffer& ApplyTransform(const Matrix4x4& Mat);
357  /// @brief Applies the translation immediately to all the points contained in this triangle buffer.
358  /// @param Trans The amount of movement to be applied on each axis.
359  /// @return Returns a reference to this.
360  TriangleBuffer& TranslateVertices(const Vector3& Trans);
361  /// @brief Applies the translation immediately to all the points contained in this triangle buffer.
362  /// @param X The amount of movement to be applied to each Vertex in this buffer on the X axis.
363  /// @param Y The amount of movement to be applied to each Vertex in this buffer on the Y axis.
364  /// @param Z The amount of movement to be applied to each Vertex in this buffer on the Z axis.
365  /// @return Returns a reference to this.
366  TriangleBuffer& TranslateVertices(const Real X, const Real Y, const Real Z);
367  /// @brief Applies the rotation immediately to all the points contained in this triangle buffer.
368  /// @param Rot The amount of rotation to be applied to each Vertex.
369  /// @return Returns a reference to this.
370  TriangleBuffer& RotateVertices(const Quaternion& Rot);
371  /// @brief Applies an immediate scale operation to this triangle buffer.
372  /// @param Scale The amount of scaling to be applied to each Vertex.
373  /// @return Returns a reference to this.
374  TriangleBuffer& ScaleVertices(const Vector3& Scale);
375  /// @brief Applies an immediate scale operation to this triangle buffer.
376  /// @param X The amount to scale the contents of this buffer on the X axis.
377  /// @param Y The amount to scale the contents of this buffer on the Y axis.
378  /// @param Z The amount to scale the contents of this buffer on the Z axis.
379  /// @return Returns a reference to this.
380  TriangleBuffer& ScaleVertices(const Real X, const Real Y, const Real Z);
381  /// @brief Applies normal inversion on this triangle buffer.
382  /// @return Returns a reference to this.
383  TriangleBuffer& InvertNormals();
384 
385  ///////////////////////////////////////////////////////////////////////////////
386  // Index Management
387 
388  /// @brief Adds an index to the index buffer.
389  /// @note Index is relative to the latest call to RebaseOffset().
390  /// @param Index The Index to be added.
391  /// @return Returns a reference to this.
392  TriangleBuffer& AddIndex(const Integer Index);
393  /// @brief Adds a triangle to the index buffer.
394  /// @note Index is relative to the latest call to RebaseOffset().
395  /// @param Index1 The first point of the triangle to be added.
396  /// @param Index2 The second point of the triangle to be added.
397  /// @param Index3 The third point of the triangle to be added.
398  /// @return Returns a reference to this.
399  TriangleBuffer& AddTriangle(const Integer Index1, const Integer Index2, const Integer Index3);
400  };//TriangleBuffer
401  }//Procedural
402  }//Graphics
403 }//Mezzanine
404 
405 #endif
std::vector< Vertex > VertexContainer
Basic container type for Vertex storage.
Whole LastIndex
The end indicy of this section in the parent buffer.
SectionContainer::iterator SectionIterator
Iterator type for Section instances stored in this class.
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
IndexContainer Indices
Container storing all of the Indices in this buffer.
VertexContainer::iterator VertexIterator
Iterator type for Vertex instances in a VertexContainer.
BufferSection CurrentSection
Temporary storage for the current section being worked on.
A convenience buffer that stores vertices and indices of a mesh to be generated.
This class is used to check and modify the properties of a graphics mesh.
Definition: mesh.h:63
int Integer
A datatype used to represent any integer close to.
Definition: datatypes.h:154
A simple definition for a Vertex to be used when procedurally generating meshes.
A 4x4 matrix math class for the representation of full transforms.
Definition: matrix4x4.h:59
Integer EstimatedIndexCount
The user estimated Index count for this buffer.
String MaterialGroup
The asset group where the material can be found.
Whole LastVertex
The end vertex of this section in the parent buffer.
Vector3 Position
The position of the vertex in local space.
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
void Clear()
Clears all the data on this BufferSection back to a blank initialization.
Integer EstimatedVertexCount
The user estimated Vertex count for this buffer.
std::vector< BufferSection > SectionContainer
Basic container type for Section storage in this class.
std::vector< Integer > IndexContainer
A container of Integers used to represent the indicies of a shape.
VertexContainer Vertices
Container storing all of the Vertices in this buffer.
Vector2 UV
The UV coordinate for applying textures.
Whole FirstIndex
The start indicy of this section in the parent buffer.
String MaterialName
The name of the material to be used with this section.
SectionContainer::const_iterator ConstSectionIterator
Const Iterator type for Section instances stored in this class.
This is used to represent a point on a 2 dimentional area, such as a screen.
Definition: vector2.h:63
Vertex(const Vector3 &Pos, const Vector3 &Norm, const Vector2 &TexCoord)
Detailed constructor.
Vertex * CurrentVertex
Convenience pointer to the Vertex currently being manipulated.
Integer GlobalOffset
Offset used to set where buffer/mesh appends begin.
Whole RenderOp
The render operation to be used for the section. See Graphics::RenderOperation enum for more info...
A convenience class used to describe a small portion of the mesh/buffer.
This is used to represent a point in space, or a vector through space.
Definition: vector3.h:77
VertexContainer::const_iterator ConstVertexIterator
Const Iterator type for Vertex instances in a VertexContainer.
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
void operator=(const BufferSection &Other)
Assignment operator.
Vector3 Normal
The facing direction of the vertex in local space.
This is used to store information about rotation in 3d space.
Definition: quaternion.h:68
SectionContainer Sections
Container storing all of the sections of this buffer.
Whole FirstVertex
The start vertex of this section in the parent buffer.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
Vertices form triangles. Each triangle has it's own distinct set of Vertices. Vertices are not expect...