Spinning Topp Logo BlackTopp Studios
inc
crackmodifier.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 _graphicsproceduralcrackmodifier_cpp
68 #define _graphicsproceduralcrackmodifier_cpp
69 
70 #include "Graphics/Procedural/Texture/crackmodifier.h"
71 
72 #include "MathTools/mathtools.h"
73 
74 namespace Mezzanine
75 {
76  namespace Graphics
77  {
78  namespace Procedural
79  {
81  CrackColour(1.0,1.0,1.0,1.0),
82  NormalsTexture(NULL),
83  CrackCount(100),
84  GeneratorSeed(5120),
85  CrackLengthMode(CrackModifier::LM_Constant),
86  CrackQualityMode(CrackModifier::QM_LowQuality),
87  CrackLength(255),
88  CrackVariation(64)
89  { }
90 
92  { }
93 
94  ///////////////////////////////////////////////////////////////////////////////
95  // Utility
96 
98  {
99  if( this->NormalsTexture == NULL ) {
100  return;
101  }
102 
103  MathTools::MersenneTwisterGenerator32 NumGen(this->GeneratorSeed);
104 
105  Whole TargetWidth = Buffer.GetWidth();
106  Whole TargetHeight = Buffer.GetHeight();
107  Whole NormalsWidth = this->NormalsTexture->GetWidth();
108  Whole NormalsHeight = this->NormalsTexture->GetHeight();
109 
110  if( NormalsWidth < TargetWidth || NormalsHeight < TargetHeight ) {
111  return;
112  }
113 
114  TextureBuffer TempBuffer( Buffer );
115 
116  for( Whole CurrCrack = 0 ; CurrCrack < this->CrackCount ; ++CurrCrack )
117  {
118  Real X = static_cast<Real>( NumGen.GenerateScaledReal() ) * static_cast<Real>( TargetWidth );
119  Real Y = static_cast<Real>( NumGen.GenerateScaledReal() ) * static_cast<Real>( TargetHeight );
120  Real Angle = MathTools::GetTwoPi() * static_cast<Real>( NumGen.GenerateScaledReal() );
121  Whole PixelCount = static_cast<Whole>( this->CrackLength );
122  ColourValue NormalsPixel = this->NormalsTexture->GetPixel( static_cast<Whole>(X), static_cast<Whole>(Y) );
123 
124  if( NormalsWidth && this->CrackLengthMode == CrackModifier::LM_NormalBased ) {
125  Vector2 Normal(NormalsPixel.RedChannel * 255.0 - 127.0, NormalsPixel.GreenChannel * 255.0 - 127.0);
126  Real NormalLength = Normal.SquaredLength();
127  NormalLength = ( NormalLength > 0 ) ? MathTools::Sqrt(NormalLength) : 0;
128  PixelCount = std::min<Whole>( static_cast<Whole>( PixelCount * NormalLength * NormalLength / 8.0 ), static_cast<Whole>( this->CrackLength ) );
129  }else if( this->CrackLengthMode == CrackModifier::LM_Random ) {
130  PixelCount = static_cast<Whole>( PixelCount * ( static_cast<Real>( NumGen.GenerateScaledReal() ) * 2.0 ) );
131  }
132 
133  while( --PixelCount >= 0 )
134  {
135  Angle += static_cast<Real>( this->CrackVariation ) / 256.0 * ( 2.0 * ( static_cast<Real>( NumGen.GenerateScaledReal() ) - 1.0 ) );
136 
137  X = X + MathTools::Cos( Angle );
138  Y = Y + MathTools::Sin( Angle );
139  if( static_cast<Whole>(X) >= TargetWidth || static_cast<Whole>(Y) >= TargetHeight )
140  break;
141 
142  if( NormalsWidth ) {
143  Vector2 Normal(127.0 - NormalsPixel.RedChannel * 255.0, NormalsPixel.GreenChannel * 255.0 - 127.0 );
144  if( Normal.X == 0.0 ) {
145  if( Normal.Y > 0.0 ) {
146  Angle = MathTools::GetPi();
147  }else{
148  Angle = MathTools::GetTwoPi();
149  }
150  }else if( Normal.X < 0 ) {
151  Angle = MathTools::ATan( Normal.Y / Normal.X ) + 1.5 * MathTools::GetPi();
152  }else if( Normal.Y < 0 ) {
153  Angle = MathTools::ATan( Normal.Y / Normal.X ) + 2.5 * MathTools::GetPi();
154  }else{
155  Angle = MathTools::ATan( Normal.Y / Normal.X ) + MathTools::GetHalfPi();
156  }
157  Real NormalLength = Normal.SquaredLength();
158  NormalLength = ( NormalLength > 0 ) ? MathTools::Sqrt(NormalLength) : 0;
159  if( NormalLength < ( 255.0 - NormalsPixel.AlphaChannel * 255.0 ) / 4.0 ) {
160  continue;
161  }
162  }
163 
164  switch( this->CrackQualityMode )
165  {
167  {
168  ColourValue x1, y1, x2, y2;
169  Real cy2, cy1, cx2, cx1;
170  Whole NextX, NextY;
171 
172  cy2 = ( X - MathTools::Floor(X) ) * ( Y - MathTools::Floor(Y) );
173  cy1 = ( Y - MathTools::Floor(Y) ) * ( MathTools::Ceil(X) - X );
174  cx2 = ( X - MathTools::Floor(X) ) * ( MathTools::Ceil(Y) - Y );
175  cx1 = 1 - ( cx2 + cy1 + cy2 );
176  NextX = std::min( static_cast<Whole>(X) + 1, TargetWidth );
177  NextY = std::min( static_cast<Whole>(Y) + 1, TargetHeight );
178 
179  x1 = Buffer.GetPixel( static_cast<Whole>(X), static_cast<Whole>(Y) );
180  y1 = Buffer.GetPixel( static_cast<Whole>(X), NextY );
181  x2 = Buffer.GetPixel( NextX, static_cast<Whole>(Y) );
182  y2 = Buffer.GetPixel( NextX, NextY );
183 
184  x1 *= ( 1 - cx1 );
185  x2 *= ( 1 - cx2 );
186  y1 *= ( 1 - cy1 );
187  y2 *= ( 1 - cy2 );
188 
189  x1 += this->CrackColour * cx1;
190  y1 += this->CrackColour * cy1;
191  x2 += this->CrackColour * cx2;
192  y2 += this->CrackColour * cy2;
193 
194  TempBuffer.SetPixel( static_cast<Whole>(X), static_cast<Whole>(Y), x1 );
195  TempBuffer.SetPixel( static_cast<Whole>(X), NextY, y1 );
196  TempBuffer.SetPixel( NextX, static_cast<Whole>(Y), x2 );
197  TempBuffer.SetPixel( NextX, NextY, y2 );
198  break;
199  }
201  {
202  TempBuffer.SetPixel( static_cast<Whole>(X), static_cast<Whole>(Y), Buffer.GetPixel( static_cast<Whole>(X), static_cast<Whole>(Y) ) + this->CrackColour );
203  break;
204  }
205  default:
206  {
207  TempBuffer.SetPixel( static_cast<Whole>(X), static_cast<Whole>(Y), this->CrackColour );
208  break;
209  }
210  }
211  }
212  }
213 
214  Buffer.SetData(TempBuffer);
215  }
216 
218  { return "CrackModifier"; }
219 
220  ///////////////////////////////////////////////////////////////////////////////
221  // Configuration
222 
224  {
225  this->NormalsTexture = Normals;
226  return *this;
227  }
228 
230  {
231  this->CrackColour = Colour;
232  return *this;
233  }
234 
235  CrackModifier& CrackModifier::SetColour(const Real Red, const Real Green, const Real Blue, const Real Alpha)
236  {
237  this->CrackColour.SetValues(Red,Green,Blue,Alpha);
238  return *this;
239  }
240 
242  {
243  this->CrackCount = Count;
244  return *this;
245  }
246 
248  {
249  this->CrackLengthMode = Length;
250  return *this;
251  }
252 
254  {
255  this->CrackQualityMode = Quality;
256  return *this;
257  }
258 
260  {
261  this->CrackLength = Length;
262  return *this;
263  }
264 
266  {
267  this->CrackVariation = Variation;
268  return *this;
269  }
270 
272  {
273  this->GeneratorSeed = Seed;
274  return *this;
275  }
276  }//Procedural
277  }//Graphics
278 }//Mezzanine
279 
280 #endif
ColourChannelType & GetPixel(const Whole X, const Whole Y, const UInt16 Component)
Gets access to the pixel at the specified position in this buffer. the X or Y location go beyond the ...
UInt8 CrackVariation
Determines the amount of "stutter" to the cracks normal curve (or non-curve depending on the paramete...
virtual String GetName() const
Gets the name of this modifier.
CrackModifier & SetCrackLengthMode(const LengthMode Length)
Sets any additional configuration for the length of the cracks.
CrackModifier & SetColour(const ColourValue &Colour)
Sets the colour of the generated cracks.
Whole GetWidth() const
Get the pixel width of this texture.
QualityMode CrackQualityMode
The mode in which the colours at or near the crack are to be determined. See QualityMode enum for mor...
The modifier will generate the pixel length of a crack between the configured value and a value large...
Definition: crackmodifier.h:94
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
CrackModifier & SetCrackQualityMode(const QualityMode Quality)
Sets any additional configuration for the colour/sampling of the crack colour.
CrackModifier & SetCrackCount(const Whole Count)
Sets how many cracks to create.
CrackModifier & SetNormalsTexture(TextureBuffer *Normals)
Sets the normals texture to use for generating cracks.
CrackModifier & SetCrackVariation(const UInt8 Variation)
Sets the amount of wobble or stutter to the cracks that are created.
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
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 Y
Coordinate on the Y vector.
Definition: vector2.h:69
Whole CrackCount
The number of cracks to generate.
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
UInt8 CrackLength
The pixel length of the cracks to be generated.
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
TextureBuffer * NormalsTexture
The parameter image to pull normals the cracks are to move along from. Alpha channel and colours may ...
Real SquaredLength() const
Gets the length of this vector squared.
Definition: vector2.cpp:260
Whole GeneratorSeed
The seed for the random number generator used to place the cracks.
void SetValues(const Real Red, const Real Green, const Real Blue, const Real Alpha)
Sets each of the colour channels.
CrackModifier & SetCrackLength(const UInt8 Length)
Sets the length of each crack that is created. May not be used verbatim.
The modifier will randomize the pixel length of the cracks between zero and double the configured len...
Definition: crackmodifier.h:93
LengthMode
An enum used to set how the length of each generated crack is to be determined.
Definition: crackmodifier.h:90
virtual void Modify(TextureBuffer &Buffer)
Alters the generated pixels in a TextureBuffer.
A convenience buffer that stores pixel colour values of a texture to be generated.
Definition: texturebuffer.h:86
Attempts to blend the colours of all surrounding pixels for a more smooth appearance.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
LengthMode CrackLengthMode
The mode in which the final length of each crack is determined. See LengthMode enum for more details...
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
Combines the colours from the image being modified and the configured crack colour.
Whole GetHeight() const
Get the pixel height of this texture.
void SetPixel(const Whole X, const Whole Y, const ColourValue &Colour)
Set colour of a specified pixel using a ColourValue. the X or Y location go beyond the set size of th...
A modifier that will generate random coloured lines on a texture.
Definition: crackmodifier.h:86
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
ColourValue CrackColour
The base colour to give to the pixels forming the cracks.
CrackModifier & SetGeneratorSeed(const Whole Seed)
Sets the seed for the random number generator used to determine crack starting positions.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
void SetData(const TextureBuffer &Other)
Copies image data from another buffer into this buffer.
QualityMode
An enum used to set the quality.
Definition: crackmodifier.h:97