Basic perlin noise is working
This commit is contained in:
+52
-48
@@ -21,70 +21,74 @@
|
|||||||
*/
|
*/
|
||||||
#include "map_generator.hpp"
|
#include "map_generator.hpp"
|
||||||
|
|
||||||
#include "maths.hpp"
|
#include "vector2.hpp"
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
//it's either this, or dealing with floating points
|
//-------------------------
|
||||||
struct Coord {
|
//Utility functions
|
||||||
int x, y;
|
//-------------------------
|
||||||
};
|
|
||||||
|
|
||||||
//std::abs() only uses floating points
|
//snap to a grid (floating point version)
|
||||||
int absolute(int i) {
|
static double snap(double x, double base) {
|
||||||
return i > 0 ? i : -i;
|
return floor(x / base) * base;
|
||||||
}
|
}
|
||||||
|
|
||||||
//calculate how far away a query point is from a grid point
|
//A.K.A.: the dot product
|
||||||
Coord calcDist(Coord gridPoint, Coord queryPoint) {
|
static double scalarProduct(Vector2 lhs, Vector2 rhs) {
|
||||||
int i = absolute(queryPoint.x - gridPoint.x);
|
return lhs.x * rhs.x + lhs.y * rhs.y;
|
||||||
int j = absolute(queryPoint.y - gridPoint.y);
|
|
||||||
return {i, j};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//calculate the amount of influence a distance will have within a certain sized area
|
//curved interpolation
|
||||||
int calcInfluence(Coord dist, int width, int height) {
|
static double curve(double x) {
|
||||||
//inverted distance
|
//param: 0 to 1 inclusive
|
||||||
Coord i = {width - dist.x, height - dist.y};
|
return 3.0 * pow(x, 2.0) - 2.0 * pow(x, 3.0);
|
||||||
//essentially calculating the length
|
|
||||||
return sqrt(i.x*i.x + i.y*i.y);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//determine the scaled value from the grid point
|
//-------------------------
|
||||||
int calcScale(int influence, int gridValue, int pointValue) {
|
//Public methods
|
||||||
/* TODO
|
//-------------------------
|
||||||
* (influence / {width, height}.Length()) //percentage to interpolate
|
|
||||||
* poingValue - gridValue //difference
|
Vector2 MapGenerator::RawNoise(Vector2 const& gridPoint) {
|
||||||
* return percentage * difference //scale
|
double angle = rng(gridPoint.x * 0xffff + gridPoint.y);
|
||||||
*/
|
Vector2 v = {cos(angle), sin(angle)};
|
||||||
return gridValue;
|
v.Normalize();
|
||||||
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
int MapGenerator::Noise(int x, int y, int width, int height) {
|
double MapGenerator::Influence(Vector2 const& gridPoint, Vector2 const& queryPoint, double width, double height) {
|
||||||
Coord queryPoint = {x, y};
|
//note: inverting the distance here, so the smaller the distance the more influence it has
|
||||||
|
Vector2 distance = queryPoint - gridPoint;
|
||||||
|
Vector2 inverted = {width - distance.x, height - distance.y};
|
||||||
|
double ret = scalarProduct(RawNoise(gridPoint), inverted);
|
||||||
|
return ret > 0 ? ret : -ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
double MapGenerator::ScaledNoise(double x, double y, double width, double height) {
|
||||||
|
Vector2 queryPoint = {x, y};
|
||||||
|
|
||||||
//the "grid points"
|
//the "grid points"
|
||||||
Coord tl = {snap(x, width), snap(y, height)};
|
Vector2 tl = {snap(x, width), snap(y, height)};
|
||||||
Coord tr = {tl.x + width, tl.y};
|
Vector2 tr = {tl.x + width, tl.y};
|
||||||
Coord bl = {tl.x, tl.y + height};
|
Vector2 bl = {tl.x, tl.y + height};
|
||||||
Coord br = {tl.x + width, tl.y + height};
|
Vector2 br = {tl.x + width, tl.y + height};
|
||||||
|
|
||||||
//get the distance from the query, and subsequently the influence
|
//influence equasion
|
||||||
int tlInfluence = calcInfluence(calcDist(tl, queryPoint), width, height);
|
double s = Influence(tl, queryPoint, width, height);
|
||||||
int trInfluence = calcInfluence(calcDist(tr, queryPoint), width, height);
|
double t = Influence(tr, queryPoint, width, height);
|
||||||
int blInfluence = calcInfluence(calcDist(bl, queryPoint), width, height);
|
double u = Influence(bl, queryPoint, width, height);
|
||||||
int brInfluence = calcInfluence(calcDist(br, queryPoint), width, height);
|
double v = Influence(br, queryPoint, width, height);
|
||||||
|
|
||||||
//now combine the grid's values with the influence values
|
//Finally, calc the value
|
||||||
int tlScale = calcScale(tlInfluence, RawNoise(tl.x, tl.y), RawNoise(queryPoint.x, queryPoint.y));
|
double a = s + curve((t - s) / width);
|
||||||
int trScale = calcScale(trInfluence, RawNoise(tr.x, tr.y), RawNoise(queryPoint.x, queryPoint.y));
|
double b = u + curve((v - u) / width);
|
||||||
int blScale = calcScale(blInfluence, RawNoise(bl.x, bl.y), RawNoise(queryPoint.x, queryPoint.y));
|
return curve((b - a) / height);
|
||||||
int brScale = calcScale(brInfluence, RawNoise(br.x, br.y), RawNoise(queryPoint.x, queryPoint.y));
|
|
||||||
|
|
||||||
//Finally, apply each scale to the raw value, resulting in the height map
|
|
||||||
return tlScale + trScale + blScale + brScale + RawNoise(queryPoint.x, queryPoint.y);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int MapGenerator::RawNoise(int x, int y) {
|
double MapGenerator::ScaleOctave(double x, double y, double width, double height, double octave) {
|
||||||
return (x * 11235 + y * 81321 + 3455) % 256;
|
double ret = 0;
|
||||||
|
if (octave > 1) {
|
||||||
|
ret += ScaleOctave(x, y, width/2, height/2, octave-1);
|
||||||
|
}
|
||||||
|
return ret / octave + ScaledNoise(x, y, width, height);
|
||||||
}
|
}
|
||||||
@@ -24,13 +24,17 @@
|
|||||||
|
|
||||||
#include "simple_rng.hpp"
|
#include "simple_rng.hpp"
|
||||||
|
|
||||||
|
#include "vector2.hpp"
|
||||||
|
|
||||||
class MapGenerator {
|
class MapGenerator {
|
||||||
public:
|
public:
|
||||||
MapGenerator() = default;
|
MapGenerator() = default;
|
||||||
~MapGenerator() = default;
|
~MapGenerator() = default;
|
||||||
|
|
||||||
int Noise(int x, int y, int width, int height);
|
Vector2 RawNoise(Vector2 const&);
|
||||||
int RawNoise(int x, int y);
|
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);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SimpleRNG rng;
|
SimpleRNG rng;
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
All coordinates have a random value assigned to them
|
|
||||||
+32
-6
@@ -27,27 +27,53 @@
|
|||||||
//Public access members
|
//Public access members
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
|
static double max = 0;
|
||||||
|
static double min = 0;
|
||||||
|
|
||||||
void setPixel(SDL_Surface* const dest, int x, int y, int colour) {
|
void setPixel(SDL_Surface* const dest, int x, int y, int colour) {
|
||||||
*(static_cast<int*>(dest->pixels) + dest->w * y + x) = colour;
|
*(static_cast<int*>(dest->pixels) + dest->w * y + x) = colour;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int convertToColour(SDL_PixelFormat* format, double x) {
|
||||||
|
//track the max value
|
||||||
|
max = x > max ? x : max;
|
||||||
|
min = x < min ? x : min;
|
||||||
|
|
||||||
|
if (x > 1) {
|
||||||
|
return SDL_MapRGB(format, 255, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (x < 0) {
|
||||||
|
return SDL_MapRGB(format, 0, 255, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (x == 0) {
|
||||||
|
return SDL_MapRGB(format, 0, 0, 255);
|
||||||
|
}
|
||||||
|
|
||||||
|
return SDL_MapRGB(format, 255*x, 255*x, 255*x);
|
||||||
|
}
|
||||||
|
|
||||||
ShellScene::ShellScene() {
|
ShellScene::ShellScene() {
|
||||||
//test the generator
|
//test the generator
|
||||||
int width = 80;
|
int width = 256;
|
||||||
int height = 80;
|
int height = 256;
|
||||||
image.CreateSurface(GetScreen()->w, GetScreen()->h);
|
image.CreateSurface(GetScreen()->w, GetScreen()->h);
|
||||||
|
|
||||||
int value = 0;
|
double value;
|
||||||
int colour = 0;
|
int colour;
|
||||||
|
|
||||||
std::cout << "Beggining generation" << std::endl;
|
std::cout << "Beggining generation" << std::endl;
|
||||||
for (int i = 0; i < image.GetSurface()->w; i++) {
|
for (int i = 0; i < image.GetSurface()->w; i++) {
|
||||||
for (int j = 0; j < image.GetSurface()->h; j++) {
|
for (int j = 0; j < image.GetSurface()->h; j++) {
|
||||||
value = generator.Noise(i, j, width, height);
|
value = generator.ScaleOctave(i, j, width, height, 8);
|
||||||
colour = SDL_MapRGB(image.GetSurface()->format, value, value, value);
|
colour = convertToColour(image.GetSurface()->format, value);
|
||||||
setPixel(image.GetSurface(), i, j, colour);
|
setPixel(image.GetSurface(), i, j, colour);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::cout << "Finished generation" << std::endl;
|
std::cout << "Finished generation" << std::endl;
|
||||||
|
std::cout << "Max: " << max << std::endl;
|
||||||
|
std::cout << "Min: " << min << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
ShellScene::~ShellScene() {
|
ShellScene::~ShellScene() {
|
||||||
|
|||||||
@@ -22,20 +22,21 @@
|
|||||||
#ifndef SIMPLERNG_HPP_
|
#ifndef SIMPLERNG_HPP_
|
||||||
#define SIMPLERNG_HPP_
|
#define SIMPLERNG_HPP_
|
||||||
|
|
||||||
|
//a simple, stateless, random number generator
|
||||||
class SimpleRNG {
|
class SimpleRNG {
|
||||||
public:
|
public:
|
||||||
SimpleRNG() { SetSeed(8891); }
|
SimpleRNG() { SetSeed(8891.0); }
|
||||||
SimpleRNG(int x) { SetSeed(x); }
|
SimpleRNG(double x) { SetSeed(x); }
|
||||||
|
|
||||||
int SetSeed(int s) { return seed = s & 0xFFFF; }
|
double SetSeed(double s) { return seed = s; }
|
||||||
int GetSeed() { return seed; }
|
double GetSeed() { return seed; }
|
||||||
|
|
||||||
int operator()(int x) {
|
double operator()(double x) {
|
||||||
return SetSeed((x + seed) * 11235 + 81321);
|
return (x + seed) * 11235.0 + 81321.0;
|
||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int seed;
|
double seed;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user