2013-05-05 13:37:06 +00:00
# include "paintingbrush.h"
# include <cmath>
2013-11-12 20:34:35 +00:00
# include <QAbstractSlider>
2013-11-03 14:46:39 +00:00
# include "NoiseGenerator.h"
2013-11-17 21:36:18 +00:00
# include "TerrainDefinition.h"
# include "TerrainHeightMap.h"
2014-09-15 10:32:27 +00:00
# include "PaintedGridBrush.h"
2013-05-05 13:37:06 +00:00
PaintingBrush : : PaintingBrush ( )
{
_mode = PAINTING_BRUSH_RAISE ;
_size = 0.0 ;
_smoothing = 0.0 ;
_strength = 0.0 ;
2013-05-05 16:42:55 +00:00
_height = 0.0 ;
2013-11-03 14:46:39 +00:00
_noise = new NoiseGenerator ( ) ;
_noise - > addLevelsSimple ( 10 , 1.0 , - 0.5 , 0.5 , 0.5 ) ;
2013-05-05 13:37:06 +00:00
}
PaintingBrush : : ~ PaintingBrush ( )
{
2013-11-03 14:46:39 +00:00
delete _noise ;
2013-05-05 13:37:06 +00:00
}
void PaintingBrush : : setMode ( PaintingBrushMode mode )
{
_mode = mode ;
}
void PaintingBrush : : setSize ( double value )
{
_size = value ;
}
void PaintingBrush : : setSize ( QAbstractSlider * slider )
{
2013-06-20 15:56:15 +00:00
double value = ( double ) slider - > value ( ) / ( double ) slider - > maximum ( ) ;
setSize ( 2.0 + value * value * 28.0 ) ;
2013-05-05 13:37:06 +00:00
}
void PaintingBrush : : setSmoothing ( double value )
{
_smoothing = value ;
}
void PaintingBrush : : setSmoothing ( QAbstractSlider * slider )
{
setSmoothing ( ( double ) slider - > value ( ) / ( double ) slider - > maximum ( ) ) ;
}
void PaintingBrush : : setStrength ( double value )
{
_strength = value ;
}
void PaintingBrush : : setStrength ( QAbstractSlider * slider )
{
2013-06-20 15:56:15 +00:00
setStrength ( 0.1 + 0.9 * ( double ) slider - > value ( ) / ( double ) slider - > maximum ( ) ) ;
2013-05-05 13:37:06 +00:00
}
void PaintingBrush : : randomizeNoise ( )
{
2013-11-03 14:46:39 +00:00
_noise - > randomizeOffsets ( ) ;
2013-05-05 13:37:06 +00:00
}
double PaintingBrush : : getInfluence ( double relative_x , double relative_z )
{
double distance = sqrt ( relative_x * relative_x + relative_z * relative_z ) ;
if ( distance > _size )
{
return 0.0 ;
}
else if ( distance > _size * ( 1.0 - _smoothing ) )
{
return 1.0 - ( distance - _size * ( 1.0 - _smoothing ) ) / ( _size * _smoothing ) ;
}
else
{
return 1.0 ;
}
}
2013-10-30 11:46:18 +00:00
void PaintingBrush : : drawPreview ( QWidget * )
2013-05-05 13:37:06 +00:00
{
}
2013-05-05 16:42:55 +00:00
QString PaintingBrush : : getHelpText ( )
{
switch ( _mode )
{
case PAINTING_BRUSH_RAISE :
return QObject : : tr ( " <strong>Left click</strong>: raise terrain<br><br><strong>Right click</strong>: lower terrain " ) ;
case PAINTING_BRUSH_SMOOTH :
return QObject : : tr ( " <strong>Left click</strong>: add random noise to terrain<br><br><strong>Right click</strong>: smooth details " ) ;
case PAINTING_BRUSH_FLATTEN :
return QObject : : tr ( " <strong>Left click</strong>: flatten at height picked with right click<br><br><strong>Right click</strong>: pick height at center " ) ;
case PAINTING_BRUSH_RESTORE :
2013-06-20 15:56:15 +00:00
return QObject : : tr ( " <strong>Left click</strong>: remove your manual modifications from terrain " ) ;
2013-05-05 16:42:55 +00:00
}
return QString ( " " ) ;
}
2013-05-05 13:37:06 +00:00
void PaintingBrush : : applyToTerrain ( TerrainDefinition * terrain , double x , double z , double duration , bool reverse )
{
double brush_strength ;
2014-09-15 10:32:27 +00:00
double hard_radius = _size * ( 1.0 - _smoothing ) ;
double smoothed_size = _size * _smoothing ;
double total_radius = hard_radius + smoothed_size ;
PaintedGridBrush brush ( hard_radius , smoothed_size , total_radius ) ;
2013-05-05 13:37:06 +00:00
brush_strength = 0.5 * _strength * duration / 0.1 ;
switch ( _mode )
{
case PAINTING_BRUSH_RAISE :
if ( reverse )
{
brush_strength = - brush_strength ;
}
2014-09-15 10:32:27 +00:00
terrain - > height_map - > brushElevation ( brush , x , z , brush_strength * 2.0 ) ;
2013-05-05 13:37:06 +00:00
break ;
case PAINTING_BRUSH_SMOOTH :
if ( reverse )
{
2014-09-15 10:32:27 +00:00
terrain - > height_map - > brushSmooth ( brush , x , z , brush_strength * 30.0 ) ;
2013-05-05 13:37:06 +00:00
}
else
{
2014-09-15 10:32:27 +00:00
terrain - > height_map - > brushAddNoise ( brush , x , z , _noise , brush_strength * 0.3 ) ;
2013-05-05 13:37:06 +00:00
}
break ;
2013-05-05 16:42:55 +00:00
case PAINTING_BRUSH_FLATTEN :
if ( reverse )
{
2014-11-21 08:45:19 +00:00
_height = terrain - > getInterpolatedHeight ( x * terrain - > scaling , z * terrain - > scaling , false , true ) ;
2013-05-05 16:42:55 +00:00
}
else
{
2014-09-15 10:32:27 +00:00
terrain - > height_map - > brushFlatten ( brush , x , z , _height , brush_strength * 30.0 ) ;
2013-05-05 16:42:55 +00:00
}
break ;
2013-05-05 13:37:06 +00:00
case PAINTING_BRUSH_RESTORE :
2014-09-15 10:32:27 +00:00
terrain - > height_map - > brushReset ( brush , x , z , brush_strength * 30.0 ) ;
2013-05-05 13:37:06 +00:00
break ;
default :
return ;
}
}