Spinning Topp Logo BlackTopp Studios
inc
meshgenerator.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 _graphicsproceduralmeshgenerator_h
68 #define _graphicsproceduralmeshgenerator_h
69 
70 #include "vector2.h"
71 #include "vector3.h"
72 #include "quaternion.h"
73 
74 #include "Graphics/Procedural/Mesh/trianglebuffer.h"
75 
76 namespace Mezzanine
77 {
78  namespace Graphics
79  {
80  namespace Procedural
81  {
82  ///////////////////////////////////////////////////////////////////////////////
83  /// @brief A base class containing all of the utilities needed for a mesh generator.
84  /// @details
85  ///////////////////////////////////////
86  template <typename T>
88  {
89  public:
90  /// @brief A convenience enum used to describe boolean options for a generator.
92  {
93  GO_EnableNormals = 1, ///< Whether or not to produce normals when generating a mesh.
94  GO_SwitchUV = 2, ///< Whether or not to swap the UV compontents when generating a mesh.
95  GO_Transform = 4 ///< Whether or not a transform has been defined and is to be used.
96  };
97  protected:
98  /// @internal
99  /// @brief Orientation to apply the mesh.
101  /// @internal
102  /// @brief Position to apply to the mesh.
104  /// @internal
105  /// @brief Scale to apply to the mesh.
107  /// @internal
108  /// @brief Rectangle in which the texture coordinates will be placed.
110  /// @internal
111  /// @brief U tile for texture coords generation.
113  /// @internal
114  /// @brief V tile for texture coords generation.
116  /// @internal
117  /// @brief Storage for the boolean options to be used by this generator.
119  /// @internal
120  /// @brief The number of texture coordinate sets to include.
122 
123  /// @brief Adds a new point to a triangle buffer, using the format defined for that MeshGenerator.
124  /// @param Buffer The triangle buffer to update.
125  /// @param Loc The location of the new point.
126  /// @param Norm the normal of the new point.
127  /// @param UV the uv texcoord of the new point.
128  inline void AddPoint(TriangleBuffer& Buffer, const Vector3& Loc, const Vector3& Norm, const Vector2& UV) const
129  {
130  if( this->GeneratorOpts | GO_Transform ) {
131  Buffer.AddVertex(this->Position + this->Orientation * (this->Scale * Loc));
132  }else{
133  Buffer.AddVertex(Loc);
134  }
135 
136  if( this->GeneratorOpts | GO_EnableNormals ) {
137  if( this->GeneratorOpts | GO_Transform ) {
138  Buffer.SetNormal(this->Orientation * Norm);
139  }else{
140  Buffer.SetNormal(Norm);
141  }
142  }
143 
144  if( this->GeneratorOpts | GO_SwitchUV ) {
145  for( UInt8 Index = 0 ; Index < this->NumTexCoordSet ; Index++ )
146  { Buffer.SetTextureCoord( this->UVOrigin.X + UV.Y * this->UTile, this->UVOrigin.Y + UV.X * this->VTile ); }
147  }else{
148  for( UInt8 Index = 0 ; Index < this->NumTexCoordSet ; Index++ )
149  { Buffer.SetTextureCoord( this->UVOrigin.X + UV.X * this->UTile, this->UVOrigin.Y + UV.Y * this->VTile ); }
150  }
151  }
152  public:
153  /// @brief Class constructor.
155  Orientation(0,0,0,1),
156  Position(0,0,0),
157  Scale(1,1,1),
158  UVOrigin(0,0),
159  UTile(1.0),
160  VTile(1.0),
161  GeneratorOpts(GO_EnableNormals),
162  NumTexCoordSet(1)
163  { }
164  /// @brief Class destructor.
165  virtual ~MeshGenerator()
166  { }
167 
168  ///////////////////////////////////////////////////////////////////////////////
169  // Utility
170 
171  /// @brief Generates the mesh.
172  /// @param MeshName The name to give to the generated mesh.
173  /// @param MeshGroup The asset group to place the mesh in.
174  /// @param MatName The name of the material to apply to the mesh.
175  /// @param MatGroup The asset group where the material can be found.
176  /// @return Returns a pointer to the newly created Mesh.
177  Mesh* GenerateMesh(const String& MeshName, const String& MeshGroup, const String& MatName, const String& MatGroup) const
178  {
179  TriangleBuffer TriBuf;
180  this->AddToTriangleBuffer(TriBuf);
181  Mesh* NewMesh = TriBuf.GenerateMesh(MeshName,MeshGroup,MatName,MatGroup);
182  return NewMesh;
183  }
184  /// @brief Creates a TriangleBuffer with the the vertices and indices as configured in this generator.
185  /// @return Returns a new TriangleBuffer with the generated vertices and indices of this generator.
187  {
188  TriangleBuffer TriBuf;
189  this->AddToTriangleBuffer(TriBuf);
190  return TriBuf;
191  }
192  /// @brief Adds the vertices and indices as configured in this generator to a triangle buffer.
193  /// @param Buffer The buffer to append this generators vertices and indices to.
194  virtual void AddToTriangleBuffer(TriangleBuffer& Buffer) const = 0;
195 
196  ///////////////////////////////////////////////////////////////////////////////
197  // Configuration
198 
199  /// @brief Sets the U Tile.
200  /// @remarks Default UTile value is "1".
201  /// @param uTile The number by which U texture coordinates are multiplied.
202  /// @return Returns a reference to this.
203  inline T& SetUTile(const Real uTile)
204  {
205  this->UTile = uTile;
206  return static_cast<T&>(*this);
207  }
208  /// @brief Sets the V Tile.
209  /// @remarks Default VTile value is "1".
210  /// @param vTile The number by which V texture coordinates are multiplied.
211  /// @return Returns a reference to this.
212  inline T& SetVTile(const Real vTile)
213  {
214  this->VTile = vTile;
215  return static_cast<T&>(*this);
216  }
217  /// @brief Sets the texture rectangle.
218  /// @param RectLeft The texture coordinate for the left edge.
219  /// @param RectRight The texture coordinate for the right edge.
220  /// @param RectTop The texture coordinate for the top edge.
221  /// @param RectBottom The texture coordinate for the bottom.
222  /// @return Returns a reference to this.
223  inline T& SetTextureRectangle(const Real RectLeft, const Real RectRight, const Real RectTop, const Real RectBottom)
224  {
225  this->UVOrigin.SetValues(RectTop,RectLeft);
226  this->UTile = RectRight - RectLeft;
227  this->VTile = RectBottom - RectTop;
228  return static_cast<T&>(*this);
229  }
230  /// @brief Sets the number of texture coordintate sets.
231  /// @remarks Default NumTexCoordSet value is "1".
232  /// @param NumSets The number of texture coordinate sets to use.
233  /// @return Returns a reference to this.
234  inline T& SetNumTexCoordSet(const UInt8 NumSets)
235  {
236  this->NumTexCoordSet = NumSets;
237  return static_cast<T&>(*this);
238  }
239 
240  /// @brief Sets whether normals are enabled or not.
241  /// @remarks Default normals value is true.
242  /// @param EnableNormals True to generate normals as points are added to the mesh, false otherwise.
243  /// @return Returns a reference to this.
244  inline T& SetEnableNormals(Boole EnableNormals)
245  {
246  if( EnableNormals ) {
247  this->GeneratorOpts |= GO_EnableNormals;
248  }else{
249  this->GeneratorOpts &= ~GO_EnableNormals;
250  }
251  return static_cast<T&>(*this);
252  }
253  /// @brief Sets whether to switch U and V texture coordinates.
254  /// @remarks Default uvs value is false.
255  /// @param SwitchUV True to swap the UV texture coordinates as points are added to the mesh, false otherwise.
256  /// @return Returns a reference to this.
257  inline T& SetSwitchUV(Boole SwitchUV)
258  {
259  if( SwitchUV ) {
260  this->GeneratorOpts |= GO_SwitchUV;
261  }else{
262  this->GeneratorOpts &= ~GO_SwitchUV;
263  }
264  return static_cast<T&>(*this);
265  }
266 
267  ///////////////////////////////////////////////////////////////////////////////
268  // Transform
269 
270  /// @brief Sets a translation baked into the resulting mesh.
271  /// @param Loc The amount of translation to be applied on each axis.
272  /// @return Returns a reference to this.
273  inline T& SetPosition(const Vector3& Loc)
274  {
275  this->Position = Loc;
276  this->GeneratorOpts |= GO_Transform;
277  return static_cast<T&>(*this);
278  }
279  /// @brief Sets a translation baked into the resulting mesh.
280  /// @param X The amount of translation to be applied on the X axis.
281  /// @param Y The amount of translation to be applied on the Y axis.
282  /// @param Z The amount of translation to be applied on the Z axis.
283  /// @return Returns a reference to this.
284  inline T& SetPosition(const Real X, const Real Y, const Real Z)
285  {
286  this->Position.SetValues(X,Y,Z);
287  this->GeneratorOpts |= GO_Transform;
288  return static_cast<T&>(*this);
289  }
290  /// @brief Sets an orientation baked into the resulting mesh.
291  /// @param Ori The rotation to apply to a generated mesh.
292  /// @return Returns a reference to this.
293  inline T& SetOrientation(const Quaternion& Ori)
294  {
295  this->Orientation = Ori;
296  this->GeneratorOpts |= GO_Transform;
297  return static_cast<T&>(*this);
298  }
299  /// @brief Sets an orientation baked into the resulting mesh.
300  /// @param X The X component of the Axis.
301  /// @param Y The Y component of the Axis.
302  /// @param Z The Z component of the Axis.
303  /// @param W Rotation on the Axis X, Y and Z defined.
304  /// @return Returns a reference to this.
305  inline T& SetOrientation(const Real X, const Real Y, const Real Z, const Real W)
306  {
307  this->Orientation.SetValues(X,Y,Z,W);
308  this->GeneratorOpts |= GO_Transform;
309  return static_cast<T&>(*this);
310  }
311  /// @brief Sets a scale baked into the resulting mesh.
312  /// @param Scaling The amount of scaling to apply on each axis to all meshes generated by this generator.
313  /// @return Returns a reference to this.
314  inline T& SetScale(const Vector3& Scaling)
315  {
316  this->Scale = Scaling;
317  this->GeneratorOpts |= GO_Transform;
318  return static_cast<T&>(*this);
319  }
320  /// @brief Sets a uniform scale baked into the resulting mesh.
321  /// @param Scaling The amount of scaling to apply on each axis to all meshes generated by this generator.
322  /// @return Returns a reference to this.
323  inline T& SetScale(const Real Scaling)
324  {
325  this->Scale.SetValues(Scaling,Scaling,Scaling);
326  this->GeneratorOpts |= GO_Transform;
327  return static_cast<T&>(*this);
328  }
329  /// @brief Sets a scale baked into the resulting mesh.
330  /// @param X The amount of scaling to apply on the X axis to all meshes generated by this generator.
331  /// @param Y The amount of scaling to apply on the Y axis to all meshes generated by this generator.
332  /// @param Z The amount of scaling to apply on the Z axis to all meshes generated by this generator.
333  /// @return Returns a reference to this.
334  inline T& SetScale(const Real X, const Real Y, const Real Z)
335  {
336  this->Scale.SetValues(X,Y,Z);
337  this->GeneratorOpts |= GO_Transform;
338  return static_cast<T&>(*this);
339  }
340  /// @brief Resets all transforms (orientation, position and scale) that would have been applied to the mesh to their default values.
341  /// @return Returns a reference to this.
342  inline T& ResetTransforms()
343  {
344  this->GeneratorOpts &= ~GO_Transform;
345  this->Position.Zero();
346  this->Orientation.SetIdentity();
347  this->Scale.SetValues(1,1,1);
348  return static_cast<T&>(*this);
349  }
350  };//MeshGenerator
351  }//Procedural
352  }//Graphics
353 }//Mezzanine
354 
355 #endif
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
T & SetVTile(const Real vTile)
Sets the V Tile.
UInt8 NumTexCoordSet
The number of texture coordinate sets to include.
A convenience buffer that stores vertices and indices of a mesh to be generated.
TriangleBuffer & SetTextureCoord(const Real U, const Real V)
Sets the texture coordinates of the current vertex.
T & SetScale(const Real Scaling)
Sets a uniform scale baked into the resulting mesh.
This class is used to check and modify the properties of a graphics mesh.
Definition: mesh.h:63
T & SetScale(const Vector3 &Scaling)
Sets a scale baked into the resulting mesh.
TriangleBuffer & SetNormal(const Vector3 &Norm)
Sets the normal of the current vertex.
virtual ~MeshGenerator()
Class destructor.
uint8_t UInt8
An 8-bit unsigned integer.
Definition: datatypes.h:118
Mesh * GenerateMesh(const String &MeshName, const String &MeshGroup, const String &MatName="", const String &MatGroup="") const
Builds a Mesh from this buffer.
Vector3 Scale
Scale to apply to the mesh.
Mesh * GenerateMesh(const String &MeshName, const String &MeshGroup, const String &MatName, const String &MatGroup) const
Generates the mesh.
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
T & SetPosition(const Vector3 &Loc)
Sets a translation baked into the resulting mesh.
T & SetOrientation(const Quaternion &Ori)
Sets an orientation baked into the resulting mesh.
Whether or not to swap the UV compontents when generating a mesh.
Definition: meshgenerator.h:94
void SetValues(const Real &X, const Real &Y, const Real &Z, const Real &W)
Sets the individual values of this quaterion directly.
Definition: quaternion.cpp:107
Real Y
Coordinate on the Y vector.
Definition: vector2.h:69
Vector3 Position
Position to apply to the mesh.
Real X
Coordinate on the X vector.
Definition: vector2.h:67
This is used to represent a point on a 2 dimentional area, such as a screen.
Definition: vector2.h:63
T & SetSwitchUV(Boole SwitchUV)
Sets whether to switch U and V texture coordinates.
A base class containing all of the utilities needed for a mesh generator.
Definition: meshgenerator.h:87
void SetValues(const Real &x, const Real &y)
Sets the X and Y values of this vector2.
Definition: vector2.cpp:105
void SetValues(const Real &X, const Real &Y, const Real &Z)
Manually sets all the members of this vector3.
Definition: vector3.cpp:524
void AddPoint(TriangleBuffer &Buffer, const Vector3 &Loc, const Vector3 &Norm, const Vector2 &UV) const
Adds a new point to a triangle buffer, using the format defined for that MeshGenerator.
Real VTile
V tile for texture coords generation.
TriangleBuffer BuildTriangleBuffer() const
Creates a TriangleBuffer with the the vertices and indices as configured in this generator.
T & ResetTransforms()
Resets all transforms (orientation, position and scale) that would have been applied to the mesh to t...
This is used to represent a point in space, or a vector through space.
Definition: vector3.h:77
virtual void AddToTriangleBuffer(TriangleBuffer &Buffer) const =0
Adds the vertices and indices as configured in this generator to a triangle buffer.
T & SetNumTexCoordSet(const UInt8 NumSets)
Sets the number of texture coordintate sets.
void Zero()
Sets all the members of this vector3 to zero.
Definition: vector3.cpp:517
void SetIdentity()
Sets default/identity values to the members of this quaternion.
Definition: quaternion.cpp:99
Whether or not a transform has been defined and is to be used.
Definition: meshgenerator.h:95
T & SetPosition(const Real X, const Real Y, const Real Z)
Sets a translation baked into the resulting mesh.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
T & SetScale(const Real X, const Real Y, const Real Z)
Sets a scale baked into the resulting mesh.
Whether or not to produce normals when generating a mesh.
Definition: meshgenerator.h:93
T & SetUTile(const Real uTile)
Sets the U Tile.
T & SetOrientation(const Real X, const Real Y, const Real Z, const Real W)
Sets an orientation baked into the resulting mesh.
UInt8 GeneratorOpts
Storage for the boolean options to be used by this generator.
Real UTile
U tile for texture coords generation.
This is used to store information about rotation in 3d space.
Definition: quaternion.h:68
GeneratorOptions
A convenience enum used to describe boolean options for a generator.
Definition: meshgenerator.h:91
T & SetTextureRectangle(const Real RectLeft, const Real RectRight, const Real RectTop, const Real RectBottom)
Sets the texture rectangle.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
Quaternion Orientation
Orientation to apply the mesh.
TriangleBuffer & AddVertex(const Vertex &Vert)
Adds a premade Vertex to the buffer.
Vector2 UVOrigin
Rectangle in which the texture coordinates will be placed.
T & SetEnableNormals(Boole EnableNormals)
Sets whether normals are enabled or not.