Hello all,
I have been working for the past few days on a perlin noise island generator (using libnoise). What I do is generate a terrain, and then apply an island mask to it. I have added a seed to the island generation portion but I am getting similiar results each time even with a seed. Can someone help me so that I can generate a truely random island each time? Here is what I am currently getting (seeds in parenthesis):
(1233212)
(999555857)
So, as you can see, the maps are very similar. I really don't know why. If needed, here is my code:
(Steps I take)
1st Generate terrain
2nd Generate island mask
3rd Apply island mask to terrain
4th Draw the terrain
//Little program to generate test map with only 0 and 1 tile
#include <iostream>
#include <libnoise/noise.h>
#include "noiseutils.h"
#include <stdlib.h>
#include <string>
using namespace std;
using namespace noise;
int main()
{
module::Perlin myModule;
utils::NoiseMap heightMap;
utils::NoiseMapBuilderPlane heightMapBuilder;
heightMapBuilder.SetSourceModule (myModule);
heightMapBuilder.SetDestNoiseMap (heightMap);
heightMapBuilder.SetDestSize (512, 512);
heightMapBuilder.SetBounds (2.0, 6.0, 1.0, 5.0);
heightMapBuilder.Build ();
string seed;
int iseed;
//Add support for character conversion to ints and clip string to 8 numbers
cout << "Please enter a seed: ";
getline(cin,seed);
cout << seed << endl;
iseed = atoi(seed.c_str());
cout << iseed << endl;
float islandMap[512][512];
enum directions {LEFT = 0, UP, RIGHT, DOWN, NONE};
for(int count = 0; count < 1; count++)
{
//Initialize values for a new map
for (int i = 0; i < 512; i++)
{
for (int j = 0; j < 512; j++)
{
islandMap[j][i] = 0;
}
}
//Choose a random location, travel in random directions
int xRand = 0,yRand=0;
int xPos = 0,yPos=0;
srand((iseed*57)/(iseed*17));
cout << iseed;
xRand= rand()%512;
yRand= rand()%512;
xPos = xRand;
yPos = yRand;
islandMap[xRand][yRand] = 1;
directions direction = NONE;
srand((int)((iseed*iseed)/57));
for (int i = 0; i < 10000000; i++)
{
if(xPos == 511 || xPos == 0)
{
xPos = rand()%512;
islandMap[xPos][yPos] +=1;
}
if(yPos == 511 || yPos == 0)
{
yPos = rand()%512;
islandMap[xPos][yPos] +=1;
}
int oldValue;
oldValue = islandMap[xPos][yPos];
int temp;
temp = rand()%4;
direction = (directions)temp;
if(direction == LEFT)
{
xPos-=1;
}
else if(direction == UP)
yPos-=1;
else if(direction == RIGHT)
xPos+=1;
else if(direction == DOWN)
yPos+=1;
if(oldValue >= islandMap[xPos][yPos])
{
islandMap[xPos][yPos] +=1;
}
}
//find max value for normalizing
int max = 0;
int min = 0;
for (int i = 0; i < 512; i++)
{
for (int j = 0; j < 512; j++)
{
if(max < islandMap[i][j])
max = islandMap[i][j];
//cout << islandMap[i][j] << " ";
if(min > islandMap[i][j])
min = islandMap[i][j];
}
}
//Multiply values of original by new maps generated
for (int i = 0; i < 512; i++)
{
for (int j = 0; j < 512; j++)
{
float islandValue = (islandMap[i][j] - min) / (max-min);
//cout << islandValue;
float mapValue = (heightMap.GetValue(i,j) + 1) / (1+1);
float newValue = (-1 + (2/1)*(islandValue*mapValue));
heightMap.SetValue(i,j, newValue);
}
}
}
utils::RendererImage renderer;
utils::Image image;
renderer.SetSourceNoiseMap (heightMap);
renderer.SetDestImage (image);
renderer.ClearGradient ();
renderer.AddGradientPoint (-1.0000, utils::Color ( 0, 0, 255, 255)); // shallow
renderer.AddGradientPoint (-0.9701, utils::Color ( 0, 0, 255, 255)); // shallow
renderer.AddGradientPoint (-0.9700, utils::Color ( 0, 128, 255, 255)); // shore
renderer.AddGradientPoint (-0.9690, utils::Color ( 0, 128, 255, 255)); // shore
renderer.AddGradientPoint (-0.9689, utils::Color (240, 240, 64, 255)); // sand
renderer.AddGradientPoint (-0.9601, utils::Color (240, 240, 64, 255)); // sand
renderer.AddGradientPoint (-0.9600, utils::Color ( 32, 160, 0, 255)); // grass
renderer.AddGradientPoint (-0.4991, utils::Color ( 32, 160, 0, 255)); // grass
renderer.AddGradientPoint (-0.4990, utils::Color ( 0, 0, 0, 255)); // pines
renderer.AddGradientPoint (-0.4987, utils::Color ( 0, 0, 0, 255)); // pines
renderer.AddGradientPoint (-0.3986, utils::Color (96, 63, 0, 255)); // dirt
renderer.AddGradientPoint ( 0.0000, utils::Color (96, 63, 0, 255)); // dirt
renderer.AddGradientPoint ( 0.0001, utils::Color (128, 128, 128, 255)); // rock
renderer.AddGradientPoint ( 1.0000, utils::Color (128, 128, 128, 255)); // rock
renderer.EnableLight ();
renderer.SetLightContrast (3.0);
renderer.SetLightBrightness (2.0);
renderer.Render ();
utils::WriterBMP writer;
writer.SetSourceImage (image);
writer.SetDestFilename ("tutorial.bmp");
writer.WriteDestFile ();
return 0;
}
Thankyou for your help in advance!! :D