Spinning Topp Logo BlackTopp Studios
inc
cellgenerator.cpp
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 _graphicsproceduralcellgenerator_cpp
68 #define _graphicsproceduralcellgenerator_cpp
69 
70 #include "Graphics/Procedural/Texture/cellgenerator.h"
71 
72 #include "vector3.h"
73 #include "MathTools/mathtools.h"
74 
75 namespace Mezzanine
76 {
77  namespace Graphics
78  {
79  namespace Procedural
80  {
82  GenColour(1.0,1.0,1.0,1.0),
83  GenSeed(5120),
84  GenRegularity(128),
85  GenDensity(8),
86  GenMode(CellGenerator::CM_Grid),
87  GenPattern(CellGenerator::CP_Both)
88  { }
89 
91  { }
92 
93  ///////////////////////////////////////////////////////////////////////////////
94  // Utility
95 
97  {
98  Boole cfc;
99  Real Coeff;
100 
101  Whole TargetWidth = Buffer.GetWidth();
102  Whole TargetHeight = Buffer.GetHeight();
103 
104  const Real Regularity = this->GenRegularity / 255.0;
105  std::vector<Vector3> CellPoints( this->GenDensity * this->GenDensity );
106  MathTools::MersenneTwisterGenerator32 NumGen(this->GenSeed);
107 
108  for( Whole Y = 0 ; Y < this->GenDensity ; ++Y )
109  {
110  for( Whole X = 0 ; X < this->GenDensity ; ++X )
111  {
112  Real RandNum1 = NumGen.GeneratePositiveReal();//static_cast<Real>( NumGen.GenerateInt() ) / 65536.0;
113  Real RandNum2 = NumGen.GeneratePositiveReal();//static_cast<Real>( NumGen.GenerateInt() ) / 65536.0;
114  CellPoints[X + Y * this->GenDensity].X = ( X + 0.5 + ( RandNum1 - 0.5 ) * ( 1 - Regularity ) ) / this->GenDensity - 1.0 / TargetWidth;
115  CellPoints[X + Y * this->GenDensity].Y = ( Y + 0.5 + ( RandNum2 - 0.5 ) * ( 1 - Regularity ) ) / this->GenDensity - 1.0 / TargetHeight;
116  CellPoints[X + Y * this->GenDensity].Z = 0;
117  }
118  }
119 
120  for( Whole Y = 0 ; Y < TargetHeight ; ++Y )
121  {
122  for( Whole X = 0 ; X < TargetWidth ; ++X )
123  {
124  Vector3 PixelPos;
125  PixelPos.X = static_cast<Real>( X ) / static_cast<Real>( TargetWidth ),
126  PixelPos.Y = static_cast<Real>( Y ) / static_cast<Real>( TargetHeight );
127  PixelPos.Z = 0.0;
128 
129  Real MinDist = 10.0;
130  Real NextMinDist = MinDist;
131  Integer Xo = X * this->GenDensity / TargetWidth;
132  Integer Yo = Y * this->GenDensity / TargetHeight;
133  for( Integer V = -1 ; V < 2 ; ++V )
134  {
135  Integer Vo = ( ( Yo + this->GenDensity + V ) % this->GenDensity ) * this->GenDensity;
136  for( Integer U = -1 ; U < 2 ; ++U )
137  {
138  Vector3 CellPos = CellPoints[ ( ( Xo + this->GenDensity + U ) % this->GenDensity ) + Vo ];
139  if( U == -1 && X * this->GenDensity < TargetWidth )
140  { CellPos.X -= 1; }
141  if( V == -1 && Y * this->GenDensity < TargetHeight )
142  { CellPos.Y -= 1; }
143  if( U == 1 && X * this->GenDensity >= TargetWidth * ( this->GenDensity - 1 ) )
144  { CellPos.X += 1; }
145  if( V == 1 && Y * this->GenDensity >= TargetHeight * ( this->GenDensity - 1 ) )
146  { CellPos.Y += 1; }
147 
148  Real Norm = PixelPos.Distance(CellPos);
149  if( Norm < MinDist ) {
150  NextMinDist = MinDist;
151  MinDist = Norm;
152  }else if( Norm < NextMinDist ) {
153  NextMinDist = Norm;
154  }
155  }
156  }
157 
158  switch( this->GenPattern )
159  {
161  {
162  MinDist = MathTools::Clamp( 2 * NextMinDist * this->GenDensity - 1, Real(0.0), Real(1.0) );
163  break;
164  }
166  {
167  MinDist = MathTools::Clamp( 1 - MinDist * this->GenDensity, Real(0.0), Real(1.0) );
168  break;
169  }
170  default:
172  {
173  MinDist = MathTools::Clamp( ( NextMinDist - MinDist ) * this->GenDensity, Real(0.0), Real(1.0) );
174  break;
175  }
176  }
177 
178  switch( this->GenMode )
179  {
181  {
182  cfc = ( ( Xo & 1 ) ^ ( Yo & 1 ) ) != 0;
183  Coeff = ( 1 - 2 * cfc ) / 2.5;
184  Buffer.SetRedByte( X, Y, static_cast<UInt8>( ( cfc + Coeff * MinDist ) * this->GenColour.RedChannel * 255.0 ) );
185  Buffer.SetGreenByte( X, Y, static_cast<UInt8>( ( cfc + Coeff * MinDist ) * this->GenColour.GreenChannel * 255.0 ) );
186  Buffer.SetBlueByte( X, Y, static_cast<UInt8>( ( cfc + Coeff * MinDist ) * this->GenColour.BlueChannel * 255.0 ) );
187  break;
188  }
189  default:
191  {
192  Buffer.SetRedByte( X, Y, static_cast<UInt8>( MinDist * this->GenColour.RedChannel * 255.0 ) );
193  Buffer.SetGreenByte( X, Y, static_cast<UInt8>( MinDist * this->GenColour.GreenChannel * 255.0 ) );
194  Buffer.SetBlueByte( X, Y, static_cast<UInt8>( MinDist * this->GenColour.BlueChannel * 255.0 ) );
195  break;
196  }
197  }
198  Buffer.SetAlphaReal( X, Y, this->GenColour.AlphaChannel );
199  }
200  }
201  }
202 
204  { return "CellGenerator"; }
205 
206  ///////////////////////////////////////////////////////////////////////////////
207  // Configuration
208 
210  {
211  this->GenColour = Colour;
212  return *this;
213  }
214 
215  CellGenerator& CellGenerator::SetColour(const Real Red, const Real Green, const Real Blue, const Real Alpha)
216  {
217  this->GenColour.SetValues(Red,Green,Blue,Alpha);
218  return *this;
219  }
220 
222  {
223  this->GenSeed = Seed;
224  return *this;
225  }
226 
228  {
229  this->GenRegularity = Regularity;
230  return *this;
231  }
232 
234  {
235  this->GenDensity = Density;
236  return *this;
237  }
238 
240  {
241  this->GenMode = Mode;
242  return *this;
243  }
244 
246  {
247  this->GenPattern = Pattern;
248  return *this;
249  }
250  }//Procedural
251  }//Graphics
252 }//Mezzanine
253 
254 #endif
void SetRedByte(const Whole X, const Whole Y, const ColourChannelType Red)
Sets the red colour value of a specified pixel. the X or Y location go beyond the set size of this te...
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
virtual String GetName() const
Gets the name of this generator.
Colours in the centers of each cell such that there is a cross connecting each of the four corners wi...
Definition: cellgenerator.h:95
CellGenerator & SetColour(const ColourValue &Colour)
Sets the colour of the interior of the cells.
Real X
Coordinate on the X vector.
Definition: vector3.h:85
Real Z
Coordinate on the Z vector.
Definition: vector3.h:89
virtual void AddToTextureBuffer(TextureBuffer &Buffer) const
Replaces and populates the pixels as configured in this generator to a TextureBuffer.
Cells are layed out in a normal grid pattern with their centers coloured in according to the set Cell...
Definition: cellgenerator.h:88
Whole GetWidth() const
Get the pixel width of this texture.
int Integer
A datatype used to represent any integer close to.
Definition: datatypes.h:154
ColourValue GenColour
The colour of the interior of the cells generated.
CellGenerator & SetDensity(const Whole Density)
Set the density of cells in texture.
CellMode GenMode
Describes how the cells are layed out.
Whole GenSeed
The seed to be used for the random number generator.
uint8_t UInt8
An 8-bit unsigned integer.
Definition: datatypes.h:118
This is a simple class for holding 4 reals representing the colour any give object or lightsource can...
Definition: colourvalue.h:64
CellGenerator & SetPattern(const CellPattern Pattern)
Set the cell pattern of texture.
CellGenerator & SetMode(const CellMode Mode)
Set the cell mode of texture.
Real Distance(const Vector3 &OtherVec) const
Gets the distance between this and another vector.
Definition: vector3.cpp:449
Colours in the centers of each cell such that there is a circle in the middle with increasing colour ...
Definition: cellgenerator.h:96
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
void SetGreenByte(const Whole X, const Whole Y, const ColourChannelType Green)
Sets the green colour value of a specified pixel. the X or Y location go beyond the set size of this ...
CellPattern GenPattern
Describes how the cells are coloured.
CellMode
An enum to describe how the cells are layed out.
Definition: cellgenerator.h:86
Real GreenChannel
Value from 0.0 to 1.0 representing the amount of green present in the colour. 1.0 if very green...
Definition: colourvalue.h:73
Real AlphaChannel
Value from 0.0 to 1.0 representing the transparency of the colours. 1.0 is opaque and 0...
Definition: colourvalue.h:79
CellGenerator & SetRegularity(const UInt8 Regularity)
Set the regularity of texture.
Cells are layed out in a normal grid pattern, but will alternate the cell colour and background colou...
Definition: cellgenerator.h:89
Real Y
Coordinate on the Y vector.
Definition: vector3.h:87
void SetAlphaReal(const Whole X, const Whole Y, const Real Alpha)
Sets the alpha colour value of a specified pixel. the X or Y location go beyond the set size of this ...
void SetValues(const Real Red, const Real Green, const Real Blue, const Real Alpha)
Sets each of the colour channels.
CellPattern
An enum to describe how the cells are coloured.
Definition: cellgenerator.h:92
Create a texture consisting of cells aligned in a grid, or a chessboard.
Definition: cellgenerator.h:82
A convenience buffer that stores pixel colour values of a texture to be generated.
Definition: texturebuffer.h:86
This is used to represent a point in space, or a vector through space.
Definition: vector3.h:77
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
Whole GetHeight() const
Get the pixel height of this texture.
Real BlueChannel
Value from 0.0 to 1.0 representing the amount of blue present in the colour. 1.0 if very blue...
Definition: colourvalue.h:76
Whole GenDensity
The number of oclumns and rows of cells to be generated.
Real RedChannel
Value from 0.0 to 1.0 representing the amount of red present in the colour. 1.0 if very red...
Definition: colourvalue.h:70
Whole GenRegularity
A value between 1 and 255 that determines how uniform the cells are.
CellGenerator & SetSeed(const Whole Seed)
Sets the seed for the "random" number generator.
void SetBlueByte(const Whole X, const Whole Y, const ColourChannelType Blue)
Sets the blue colour value of a specified pixel. the X or Y location go beyond the set size of this t...
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159