Add perlin noise generator

This commit is contained in:
Peder Bergebakken Sundt 2019-03-15 21:23:23 +01:00
parent 06e6ce7494
commit 0bd6cdf6a8
3 changed files with 36 additions and 1 deletions

View File

@ -75,6 +75,7 @@ double totalElapsedTime = debug_startTime;
PNGImage t_charmap;
PNGImage t_cobble_diff;
PNGImage t_cobble_normal;
PNGImage t_perlin;
void mouseCallback(GLFWwindow* window, double x, double y) {
int windowWidth, windowHeight;
@ -123,6 +124,7 @@ void initGame(GLFWwindow* window, CommandLineOptions gameOptions) {
t_charmap = loadPNGFile("../res/textures/charmap.png");
t_cobble_diff = loadPNGFile("../res/textures/cobble_diff.png");
t_cobble_normal = loadPNGFile("../res/textures/cobble_normal.png");
t_perlin = makePerlinNoisePNG(1639*2, 44, {0.1, 0.2, 0.3});
rootNode = createSceneNode();
boxNode = createSceneNode(NORMAL_TEXTURED_GEOMETRY);

View File

@ -43,3 +43,32 @@ PNGImage loadPNGFile(std::string fileName) {
return image;
}
PNGImage makePerlinNoisePNG(uint w, uint h, float scale) {
return makePerlinNoisePNG(w, h, vector<float>{scale});
}
PNGImage makePerlinNoisePNG(uint w, uint h, vector<float> scales) {
uint wb = 4*w; // in bytes
vector<unsigned char> pixels(wb*h);
for (uint y = 0; y < h; y++)
for (uint x = 0; x < w; x++) {
float v = 0;
for (float scale : scales)
v += glm::simplex(vec2(x*scale, y*scale));
v /= scales.size();
unsigned char val = (unsigned char) (127 + 128*v);
pixels[y*wb + x*4 + 0] = val;
pixels[y*wb + x*4 + 1] = val;
pixels[y*wb + x*4 + 2] = val;
pixels[y*wb + x*4 + 3] = 0xff;
}
PNGImage image;
image.width = w;
image.height = h;
image.pixels = pixels;
return image;
}

View File

@ -11,4 +11,8 @@ struct PNGImage {
std::vector<unsigned char> pixels; // RGBA
};
PNGImage loadPNGFile(std::string fileName);
PNGImage loadPNGFile(std::string fileName);
PNGImage makePerlinNoisePNG(uint w, uint h, float scale=0.1);
PNGImage makePerlinNoisePNG(uint w, uint h, std::vector<float> scales);