I am trying to wrap my head around Perlin noise.
This article has helped and I have been trying to recreate the cloud type images that it provides.
My noise code is as follows:
#include "terrain_generator.hpp"
using namespace std;
#define PI 3.1415927;
float noise(int x, int y)
{
int n = x + y * 57;
n = (n<<13) ^ n;
return (1.0 - ( (n * ((n * n * 15731) + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
}
float cosine_interpolate(float a, float b, float x)
{
float ft = x * PI;
float f = (1 - cos(ft)) * 0.5;
float result = a*(1-f) + b*f;
return result;
}
float smooth_noise_2D(float x, float y)
{
float corners = ( noise(x-1, y-1)+noise(x+1, y-1)+noise(x-1, y+1)+noise(x+1, y+1) ) / 16;
float sides = ( noise(x-1, y) +noise(x+1, y) +noise(x, y-1) +noise(x, y+1) ) / 8;
float center = noise(x, y) / 4;
return corners + sides + center;
}
float interpolated_noise(float x, float y)
{
int x_whole = (int) x;
float x_frac = x - x_whole;
int y_whole = (int) y;
float y_frac = y - y_whole;
float v1 = smooth_noise_2D(x_whole, y_whole);
float v2 = smooth_noise_2D(x_whole, y_whole+1);
float v3 = smooth_noise_2D(x_whole+1, y_whole);
float v4 = smooth_noise_2D(x_whole+1, y_whole+1);
float i1 = cosine_interpolate(v1,v3,x_frac);
float i2 = cosine_interpolate(v2,v4,x_frac);
return cosine_interpolate(i1, i2, y_frac);
}
float perlin_noise_2D(float x, float y)
{
int octaves=5;
float persistence=0.5;
float total = 0;
for(int i=0; i<octaves-1; i++)
{
float frequency = pow(2,i);
float amplitude = pow(persistence,i);
total = total + interpolated_noise(x * frequency, y * frequency) * amplitude;
}
return total;
}
To actually implement the algorithm, I am trying to make the clouds he depicted in the article.
I am using openGL and I am making my own texture and pasting it onto a quad that covers the screen. That is irrelevant though. In the code below, just know that the set pixel function works correctly and that its parameters are (x, y, red, green, blue).
This is essentially my draw loop:
for(int y=0; y<texture_height; y++)
{
for(int x=0; x<texture_width; x++)
{
seed2+=1;
float Val=perlin_noise_2D(x,y);
Val = Val/2.0;
Val = (Val + 1.0) / 2.0;
setPixel(x,y,Val,Val,Val);
}
}
What I get is the following:
How can I manipulate my algorithm to achieve the effect I am looking for? changing the persistence or number of octaves doesn't seem to do much at all.
As your result looks almost like white noise, your samples are probably too far apart within the perlin noise. Try using something smaller than the pixel coordinates to evaluate the noise at.
Something similar to this:
perlin_noise_2D((float)x/texture_width,(float)y/texture_height);
Related
I am attempting to generate noise similar to perlin or value noise.
I am using stb_image_write library from here to write to image file (i write to disk as hdr and converted to png with GIMP to post on here).
This code requires C++ 20 because I am using std::lerp();
Because I am generating a linear gradient as testing I am expecting a linear gradient as output.
I know there are more steps to generating the desired noise but, this is where I'm having issues.
#include <cmath>
template <size_t size>
void noise(float seed[size][size], float output[size][size], size_t octave);
int main()
{
// generate test gradient
float seedNoise[64][64] = {};
for ( size_t x = 0; x < 64; x++ )
{
for ( size_t y = 0; y < 64; y++ )
{
seedNoise[x][y] = ((((float)x) / 64.0f + ((float)y / 64.0f)) / 2.0f);
}
}
float _map[64][64] = { 0 };
noise<64>(seedNoise, _map, 4);
}
template <size_t size>
void noise(float seed[size][size], float output[size][size], size_t octave)
{
size_t step = size / octave; // went back to this
// size_t step = (size - 1) / octave; // took this back out
for ( size_t x = 0; x <= size - step; x += step )
{
for ( size_t y = 0; y <= size - step; y += step )
{ // each octave
// extract values at corners octave from seed
float a = seed[x][y];
float b = seed[x + (step - 1)][y]; // changed this
float c = seed[x][y + (step - 1)]; // this
float d = seed[x + (step - 1)][y + (step - 1)]; // and this
for ( size_t u = 0; u < step; u++ )
{
float uStep = ((float)u) / ((float)step); // calculate x step
for ( size_t v = 0; v < step; v++ )
{ // each element in each octave
float vStep = ((float)v) / ((float)step); // calculate y step
float x1 = std::lerp(a, b, uStep); // interpolate top edge
float x2 = std::lerp(c, d, uStep); // interpolate bottom edge
float y1 = std::lerp(a, c, vStep); // interpolate left edge
float y2 = std::lerp(b, d, vStep); // interpolate right edge
float x3 = std::lerp(x1, x2, vStep); // interpolate between top and bottom edges
float y3 = std::lerp(y1, y2, uStep); // interpolate between left and right edges
float odat = (x3 + y3) / 2; // average top/bottom and left/right interpolations
output[x + u][y + v] = odat;
}
}
}
}
}
Source gradient I think this should be similar to what the output should be.
Output As you can see here the right and bottom of the output is all messed up.
new output
I think you're accessing the imago outside it's boundaries.
X and y can go up to 60 in the loops:
for ( size_t x = 0; x <= size - step; x += step )
And the you are accessing position y+step and x+step, which gives 64.
I am trying to generate a procedural terrain using perlin noise. Before, I was just creating a 1000 * 1000 vertice terrain so I just had a simple function that would fill the height map with the noise values.
However now I am trying to generate a terrain that generates chunks as the camera moves around and I don't think this method is suitable since each chunk would be generated with it's own random heightmap and chunks will not look consistant with eachother. Instead I am using this header file to generate noise which just takes and x and y but whatever values I give it does not seem to generate consistant values and I am not sure why
class Noise {
public:
int seed;
Noise(int seed) {
this->seed = seed;
}
float noise(int x, int y) {
int n = x + y * 57 + seed;
n = ( n << 13 ) ^ n;
float rand = 1 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f;
return rand;
}
float noise(int x, int y, int z) {
int n = x + y + z * 57 + seed;
n = ( n << 13 ) ^ n;
float rand = 1 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f;
return rand;
}
float smoothNoise(int x, int y) {
float corners = (noise(x+1, y+1) + noise(x+1, y-1) + noise(x-1, y+1) + noise(x-1, y-1)) / 16.0f;
float sides = (noise(x, y+1) + noise(x, y-1) + noise(x-1, y) + noise(x+1, y)) / 8.0f;
float center = noise(x, y) / 4.0f;
return corners + sides + center;
}
float interpolatedNoise(float x, float y) {
int integerX = (int)x;
float fractionalX = x - integerX;
int integerY = (int)y;
float fractionalY = y - integerY;
float v1 = smoothNoise(integerX, integerY);
float v2 = smoothNoise(integerX + 1, integerY);
float v3 = smoothNoise(integerX, integerY + 1);
float v4 = smoothNoise(integerX + 1, integerY + 1);
float i1 = interpolateCosine(v1, v2, fractionalX);
float i2 = interpolateCosine(v3, v4, fractionalX);
return interpolateCosine(i1, i2, fractionalY);
}
// x must be in the range [0,1]
float interpolateLinear(float a, float b, float x) {
return a * (1 - x) + b * x;
}
float interpolateCosine(float a, float b, float x) {
float ft = x * (float)glm::pi<float>();
float f = (1 - ((float)glm::pi<float>())) * 0.5f;
return a * (1 - f) + b * f;
}
};`
I am calling interpolatedNoise() like this:
for(int y=x; x < config->width;x++)
{
for(int y = 0; y < config->height;y++)
{
map[x * config->height + y] = noise.interpolatedNoise((config->width * gridPosX + x) * 0.1f,(config->height * gridPosY + y) * 0.1f)/2.0f + 0.5f; // convert from -1:1 to 0:1
}
}
This is what the terrain looks like using this
but I want the terrain to be smooth. The parameters I pass into the function are divided by some value. The larger I make this value for more random the terrain is with the boxes being smaller and the smaller I make it I just get larger boxes but a more consistant looking terrain.
I am not quite sure how you are calling your Noise constructor, but one problem I see is in the constructor. It should be this->seed = seed;, otherwise you are assigning the argument seed to the value of itself. The seed variable of Noise then would just be a random number, and I feel like that's causing problems. Again, not sure how your callling Noise constructor, but thats my best guess.
I've been working on drawing the Julia set using a distance estimator instead of the normalized iteration count. I usually use the code below and play around with the iteration count until I get a decent enough picture
double Mandelbrot::getJulia(double x, double y)
{
complex<double> z(x, y);
complex<double> c(-0.7269, 0.1889);
double iterations = 0;
while (iterations < MAX)
{
z = z * z + c;
if (abs(z) > 2) {
return iterations + 1.0 - log(log2(abs(z)));
break;
}
iterations++;
}
return double(MAX);
}
I then call this for each point and draw to a bitmap;
ZoomTool zt(WIDTH, HEIGHT);
zt.add(Zoom(WIDTH / 2, HEIGHT / 2, 4.0 / WIDTH));
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
pair<double, double> coords = zt.zoomIn(x, y);
double iterations = Mandelbrot::getJulia(coords.first,
coords.second);
double ratio = iterations / Mandelbrot::MAX;
double h = 0;
double s= 0;
double v = 0;
if (ratio != 1)
{
h = 360.0*ratio;
s = 1.0;
v = 1.0;
}
HSV hsv(h, s, v);
RGB rgb(0, 0, 0);
rgb = toRGB(hsv);
bitmap.setPixel(x, y, rgb._r, rgb._g, rgb._b);
}
}
At 600 iterations, I get this;
Which is not great but better than what I get with the distance estimator which I am attempting to now use. I've implemented the distance estimator as below;
double Mandelbrot::getJulia(double x, double y)
{
complex<double> z(x,y);
complex<double> c(-0.7269, 0.1889);
complex<double> dz = 0;
double iterations = 0;
while (iterations < MAX)
{
dz = 2.0 * dz * z + 1.0;
z = z * z + c;
if (abs(z) > 2)
{
return abs(z) * log(abs(z)) / abs(dz);
}
iterations++;
}
return Mandelbrot::MAX;
}
At 600 iterations, I get the following image
Am I not normalizing the colors correctly? I'm guessing this is happening because I'm normalizing to 360.0 and doing a conversion from HSV to RGB. Since the distances are quite small, I get a very condensed distribution of colors.
I'm have this function taken from here:
bool interpolate(const Mat &im, float ofsx, float ofsy, float a11, float a12, float a21, float a22, Mat &res)
{
bool ret = false;
// input size (-1 for the safe bilinear interpolation)
const int width = im.cols-1;
const int height = im.rows-1;
// output size
const int halfWidth = res.cols >> 1;
const int halfHeight = res.rows >> 1;
int dim = res.rows * res.cols;
float *out = res.ptr<float>(0);
for (int j=-halfHeight; j<=halfHeight; ++j)
{
const float rx = ofsx + j * a12;
const float ry = ofsy + j * a22;
#pragma omp simd
for(int i=-halfWidth; i<=halfWidth; ++i)
{
float wx = rx + i * a11;
float wy = ry + i * a21;
const int x = (int) floor(wx);
const int y = (int) floor(wy);
if (x >= 0 && y >= 0 && x < width && y < height)
{
// compute weights
wx -= x; wy -= y;
// bilinear interpolation
*out++ =
(1.0f - wy) * ((1.0f - wx) * im.at<float>(y,x) + wx * im.at<float>(y,x+1)) +
( wy) * ((1.0f - wx) * im.at<float>(y+1,x) + wx * im.at<float>(y+1,x+1));
} else {
*out++ = 0;
ret = true; // touching boundary of the input
}
}
}
return ret;
}
I don't know what interpolation is in details, but looking at this opencv page, it seems that it's a bilinear interpolation using INTER_LINEAR. The point is that I don't know how to call an equivalent opencv function for the code above.
This function is called in two different points here.
You can't solely apply simple an interpolation using openCV,
it has to be part of image processing operation, e.g warp or resize operation.
I think you are trying to vectorize a warp affine, the easiest way and most efficient code on Intel platform would be to use IPP.
Otherwise, I'd let Opencv Warp affine do the job.
I tried a quick and dirty translation of the code here.
However, my version outputs noise comparable to grey t-shirt material, or heather if it please you:
#include <fstream>
#include "perlin.h"
double Perlin::cos_Interp(double a, double b, double x)
{
ft = x * 3.1415927;
f = (1 - cos(ft)) * .5;
return a * (1 - f) + b * f;
}
double Perlin::noise_2D(double x, double y)
{
/*
int n = (int)x + (int)y * 57;
n = (n << 13) ^ n;
int nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
return 1.0 - ((double)nn / 1073741824.0);
*/
int n = (int)x + (int)y * 57;
n = (n<<13) ^ n;
return ( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
}
double Perlin::smooth_2D(double x, double y)
{
corners = ( noise_2D(x - 1, y - 1) + noise_2D(x + 1, y - 1) + noise_2D(x - 1, y + 1) + noise_2D(x + 1, y + 1) ) / 16;
sides = ( noise_2D(x - 1, y) + noise_2D(x + 1, y) + noise_2D(x, y - 1) + noise_2D(x, y + 1) ) / 8;
center = noise_2D(x, y) / 4;
return corners + sides + center;
}
double Perlin::interp(double x, double y)
{
int x_i = int(x);
double x_left = x - x_i;
int y_i = int(y);
double y_left = y - y_i;
double v1 = smooth_2D(x_i, y_i);
double v2 = smooth_2D(x_i + 1, y_i);
double v3 = smooth_2D(x_i, y_i + 1);
double v4 = smooth_2D(x_i + 1, y_i + 1);
double i1 = cos_Interp(v1, v2, x_left);
double i2 = cos_Interp(v3, v4, x_left);
return cos_Interp(i1, i2, y_left);
}
double Perlin::perlin_2D(double x, double y)
{
double total = 0;
double p = .25;
int n = 1;
for(int i = 0; i < n; ++i)
{
double freq = pow(2, i);
double amp = pow(p, i);
total = total + interp(x * freq, y * freq) * amp;
}
return total;
}
int main()
{
Perlin perl;
ofstream ofs("./noise2D.ppm", ios_base::binary);
ofs << "P6\n" << 512 << " " << 512 << "\n255\n";
for(int i = 0; i < 512; ++i)
{
for(int j = 0; j < 512; ++j)
{
double n = perl.perlin_2D(i, j);
n = floor((n + 1.0) / 2.0 * 255);
unsigned char c = n;
ofs << c << c << c;
}
}
ofs.close();
return 0;
}
I don't believe that I strayed too far from the aforementioned site's directions aside from adding in the ppm image generation code, but then again I'll admit to not fully grasping what is going on in the code.
As you'll see by the commented section, I tried two (similar) ways of generating pseudorandom numbers for noise. I also tried different ways of scaling the numbers returned by perlin_2D to RGB color values. These two ways of editing the code have just yielded different looking t-shirt material. So, I'm forced to believe that there's something bigger going on that I am unable to recognize.
Also, I'm compiling with g++ and the c++11 standard.
EDIT: Here's an example: http://imgur.com/Sh17QjK
To convert a double in the range of [-1.0, 1.0] to an integer in range [0, 255]:
n = floor((n + 1.0) / 2.0 * 255.99);
To write it as a binary value to the PPM file:
ofstream ofs("./noise2D.ppm", ios_base::binary);
...
unsigned char c = n;
ofs << c << c << c;
Is this a direct copy of your code? You assigned an integer to what should be the Y fractional value - it's a typo and it will throw the entire noise algorithm off if you don't fix:
double Perlin::interp(double x, double y)
{
int x_i = int(x);
double x_left = x - x_i;
int y_i = int(y);
double y_left = y = y_i; //This Should have a minus, not an "=" like the line above
.....
}
My guess is if you're successfully generating the bitmap with the proper color computation, you're getting vertical bars or something along those lines?
You also need to remember that the Perlin generator usually spits out numbers in the range of -1 to 1 and you need to multiply the resultant value as such:
value * 127 + 128 = {R, G, B}
to get a good grayscale image.