A few tweaks

This commit is contained in:
Kayne Ruse
2014-06-21 04:05:16 +10:00
parent ccc57ec2d2
commit 8f39d58107
3 changed files with 56 additions and 4 deletions
+28 -2
View File
@@ -45,6 +45,27 @@ static double curve(double x) {
return 3.0 * pow(x, 2.0) - 2.0 * pow(x, 3.0);
}
//fix the overflow
static double curl(double x) {
if (x > 1.0) {
return -curl(x-1) +1;
}
if (x < 0.0) {
return curl(-x);
}
return x;
}
static double cut(double x) {
if (x > 1.0) {
return 1.0;
}
if (x < 0.0) {
return 0.0;
}
return x;
}
//-------------------------
//Public methods
//-------------------------
@@ -85,10 +106,15 @@ double MapGenerator::ScaledNoise(double x, double y, double width, double height
return curve((b - a) / height);
}
double MapGenerator::ScaleOctave(double x, double y, double width, double height, double octave) {
double MapGenerator::ScaledOctave(double x, double y, double width, double height, double octave) {
double ret = 0;
if (octave > 1) {
ret += ScaleOctave(x, y, width/2, height/2, octave-1);
ret += ScaledOctave(x, y, width/2, height/2, octave-1);
}
return ret / octave + ScaledNoise(x, y, width, height);
}
double MapGenerator::GetPixel(double x, double y, double width, double height, double octave) {
//use this as a decorator function
return curl(ScaledOctave(x, y, width, height, octave));
}
+2 -1
View File
@@ -34,7 +34,8 @@ public:
Vector2 RawNoise(Vector2 const&);
double Influence(Vector2 const& gridPoint, Vector2 const& queryPoint, double width, double height);
double ScaledNoise(double x, double y, double width, double height);
double ScaleOctave(double x, double y, double width, double height, double octave);
double ScaledOctave(double x, double y, double width, double height, double octave);
double GetPixel(double x, double y, double width, double height, double octave);
private:
SimpleRNG rng;
+26 -1
View File
@@ -54,6 +54,31 @@ int convertToColour(SDL_PixelFormat* format, double x) {
return SDL_MapRGB(format, 255*x, 255*x, 255*x);
}
int convertToTerrain(SDL_PixelFormat* format, double x) {
//water
if (x < 0.4) {
return SDL_MapRGB(format, 0, 0, 255);
}
//sand
if (x < 0.5) {
return SDL_MapRGB(format, 255, 255, 0);
}
//grass
if (x < 0.6) {
return SDL_MapRGB(format, 0, 255, 0);
}
//forest
if (x < 0.8) {
return SDL_MapRGB(format, 0, 128, 0);
}
//snow
if (x < 0.8) {
return SDL_MapRGB(format, 255, 255, 255);
}
//dark snow
return SDL_MapRGB(format, 128, 128, 128);
}
ShellScene::ShellScene() {
//test the generator
int width = 256;
@@ -66,7 +91,7 @@ ShellScene::ShellScene() {
std::cout << "Beggining generation" << std::endl;
for (int i = 0; i < image.GetSurface()->w; i++) {
for (int j = 0; j < image.GetSurface()->h; j++) {
value = generator.ScaleOctave(i, j, width, height, 8);
value = generator.GetPixel(i, j, width, height, 8);
colour = convertToColour(image.GetSurface()->format, value);
setPixel(image.GetSurface(), i, j, colour);
}