From 0bd6cdf6a8319d1db1010d311d057e328d953332 Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Fri, 15 Mar 2019 21:23:23 +0100 Subject: [PATCH] Add perlin noise generator --- src/gamelogic.cpp | 2 ++ src/utilities/imageLoader.cpp | 29 +++++++++++++++++++++++++++++ src/utilities/imageLoader.hpp | 6 +++++- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/gamelogic.cpp b/src/gamelogic.cpp index 97dff75..c6e421f 100644 --- a/src/gamelogic.cpp +++ b/src/gamelogic.cpp @@ -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); diff --git a/src/utilities/imageLoader.cpp b/src/utilities/imageLoader.cpp index 9a04d2f..bd4670f 100644 --- a/src/utilities/imageLoader.cpp +++ b/src/utilities/imageLoader.cpp @@ -43,3 +43,32 @@ PNGImage loadPNGFile(std::string fileName) { return image; } + + +PNGImage makePerlinNoisePNG(uint w, uint h, float scale) { + return makePerlinNoisePNG(w, h, vector{scale}); +} +PNGImage makePerlinNoisePNG(uint w, uint h, vector scales) { + uint wb = 4*w; // in bytes + + vector 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; +} diff --git a/src/utilities/imageLoader.hpp b/src/utilities/imageLoader.hpp index 5855b0e..5c65af8 100644 --- a/src/utilities/imageLoader.hpp +++ b/src/utilities/imageLoader.hpp @@ -11,4 +11,8 @@ struct PNGImage { std::vector pixels; // RGBA }; -PNGImage loadPNGFile(std::string fileName); \ No newline at end of file +PNGImage loadPNGFile(std::string fileName); + +PNGImage makePerlinNoisePNG(uint w, uint h, float scale=0.1); + +PNGImage makePerlinNoisePNG(uint w, uint h, std::vector scales);