Advertisement
spacechase0

Colored Terrain

Mar 14th, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <noise/interp.h>
  3. #include <noise/noise.h>
  4. #include <algorithm>
  5. #include <iostream>
  6.  
  7. #include "ColorScale.h"
  8.  
  9. namespace sf
  10. {
  11.     typedef Vector2< double > Vector2d;
  12. }
  13.  
  14. const int SIZE = 256;
  15. const int GRADIENT = 128;
  16.  
  17. void GetGradient( sf::Image& gradient, int height )
  18. {
  19.     ColorScale scale;
  20.     scale.insert( -1.00, sf::Color(   0,   4,  64 ) ); // Deep water
  21.     scale.insert( -0.60, sf::Color(   0,   8, 128 ) ); // Normal water
  22.     scale.insert( -0.15, sf::Color(   0,  13, 217 ) ); // Shallow water
  23.     scale.insert(  0.05, sf::Color( 253, 255, 209 ) ); // Shore
  24.     scale.insert(  0.25, sf::Color(   0, 145,   7 ) ); // Grass
  25.     scale.insert(  0.45, sf::Color(   0,  77,   4 ) ); // Trees
  26.     scale.insert(  0.65, sf::Color( 100, 100, 100 ) ); // Stone
  27.     scale.insert(  1.00, sf::Color( 255, 255, 255 ) ); // Snow
  28.    
  29.     gradient.Create( 1, height );
  30.     scale.draw( gradient, sf::Vector2f( 0.f, 0.f ), sf::Vector2f( 0.f, height ), GradientStyle::Linear );
  31. }
  32.  
  33. sf::Color GetColor( const sf::Image& gradient, double factor )
  34. {
  35.     unsigned int y = gradient.GetHeight() * ( ( factor + 1 ) / 2 );
  36.     if ( y > gradient.GetHeight() - 1 )
  37.     {
  38.         y = gradient.GetHeight() - 1;
  39.     }
  40.    
  41.     sf::Color col = gradient.GetPixel( 0, y );
  42.    
  43.     return col;
  44. }
  45.  
  46. int main()
  47. {
  48.     noise::module::Perlin perlin;
  49.     noise::model::Plane plane( perlin );
  50.    
  51.     sf::Vector2d pos( 2.D, 1.D );
  52.     sf::Vector2i size( SIZE, SIZE );
  53.     sf::Vector2d extent( 4.D, 4.D );
  54.     sf::Vector2d end( pos.x + extent.x, pos.y + extent.y );
  55.     sf::Vector2d delta( extent.x / size.x, extent.y / size.y );
  56.    
  57.     std::vector< std::vector< double > > values( size.x );
  58.     for ( std::size_t ix = 0; ix < size.x; ++ix )
  59.     {
  60.         values[ ix ].resize( size.y );
  61.     }
  62.    
  63.     for ( int ix = 0; ix < size.x; ++ix )
  64.     {
  65.         for ( int iy = 0; iy < size.y; ++iy )
  66.         {
  67.             sf::Vector2d current = pos;
  68.             current.x += delta.x * ix;
  69.             current.y += delta.y * iy;
  70.            
  71.             double value = plane.GetValue( current.x, current.y );
  72.            
  73.             values[ ix ][ iy ] = value;
  74.         }
  75.     }
  76.    
  77.     sf::Image gradient;
  78.     GetGradient( gradient, GRADIENT );
  79.    
  80.     sf::Image img;
  81.     img.Create( size.x, size.y, sf::Color::White );
  82.    
  83.     for ( int ix = 0; ix < size.x; ++ix )
  84.     {
  85.         for ( int iy = 0; iy < size.y; ++iy )
  86.         {
  87.             double value = values[ ix ][ iy ];
  88.             value = ( value < -1.D ) ? -1.D : value;
  89.             value = ( value >  1.D ) ?  1.D : value;
  90.            
  91.             img.SetPixel( ix, ( size.y - 1 ) - iy, GetColor( gradient, value ) );
  92.         }
  93.     }
  94.    
  95.     if ( !img.SaveToFile( "noise.png" ) )
  96.     {
  97.         std::cout << "Failed to save noise." << std::endl;
  98.     }
  99.     gradient.SaveToFile("gradient.png");
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement