Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <noise/interp.h>
- #include <noise/noise.h>
- #include <algorithm>
- #include <iostream>
- #include "ColorScale.h"
- namespace sf
- {
- typedef Vector2< double > Vector2d;
- }
- const int SIZE = 256;
- const int GRADIENT = 128;
- void GetGradient( sf::Image& gradient, int height )
- {
- ColorScale scale;
- scale.insert( -1.00, sf::Color( 0, 4, 64 ) ); // Deep water
- scale.insert( -0.60, sf::Color( 0, 8, 128 ) ); // Normal water
- scale.insert( -0.15, sf::Color( 0, 13, 217 ) ); // Shallow water
- scale.insert( 0.05, sf::Color( 253, 255, 209 ) ); // Shore
- scale.insert( 0.25, sf::Color( 0, 145, 7 ) ); // Grass
- scale.insert( 0.45, sf::Color( 0, 77, 4 ) ); // Trees
- scale.insert( 0.65, sf::Color( 100, 100, 100 ) ); // Stone
- scale.insert( 1.00, sf::Color( 255, 255, 255 ) ); // Snow
- gradient.Create( 1, height );
- scale.draw( gradient, sf::Vector2f( 0.f, 0.f ), sf::Vector2f( 0.f, height ), GradientStyle::Linear );
- }
- sf::Color GetColor( const sf::Image& gradient, double factor )
- {
- unsigned int y = gradient.GetHeight() * ( ( factor + 1 ) / 2 );
- if ( y > gradient.GetHeight() - 1 )
- {
- y = gradient.GetHeight() - 1;
- }
- sf::Color col = gradient.GetPixel( 0, y );
- return col;
- }
- int main()
- {
- noise::module::Perlin perlin;
- noise::model::Plane plane( perlin );
- sf::Vector2d pos( 2.D, 1.D );
- sf::Vector2i size( SIZE, SIZE );
- sf::Vector2d extent( 4.D, 4.D );
- sf::Vector2d end( pos.x + extent.x, pos.y + extent.y );
- sf::Vector2d delta( extent.x / size.x, extent.y / size.y );
- std::vector< std::vector< double > > values( size.x );
- for ( std::size_t ix = 0; ix < size.x; ++ix )
- {
- values[ ix ].resize( size.y );
- }
- for ( int ix = 0; ix < size.x; ++ix )
- {
- for ( int iy = 0; iy < size.y; ++iy )
- {
- sf::Vector2d current = pos;
- current.x += delta.x * ix;
- current.y += delta.y * iy;
- double value = plane.GetValue( current.x, current.y );
- values[ ix ][ iy ] = value;
- }
- }
- sf::Image gradient;
- GetGradient( gradient, GRADIENT );
- sf::Image img;
- img.Create( size.x, size.y, sf::Color::White );
- for ( int ix = 0; ix < size.x; ++ix )
- {
- for ( int iy = 0; iy < size.y; ++iy )
- {
- double value = values[ ix ][ iy ];
- value = ( value < -1.D ) ? -1.D : value;
- value = ( value > 1.D ) ? 1.D : value;
- img.SetPixel( ix, ( size.y - 1 ) - iy, GetColor( gradient, value ) );
- }
- }
- if ( !img.SaveToFile( "noise.png" ) )
- {
- std::cout << "Failed to save noise." << std::endl;
- }
- gradient.SaveToFile("gradient.png");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement