diff --git a/includes/noise/perlin.hpp b/includes/noise/perlin.hpp index 49a7ea1..dd84951 100644 --- a/includes/noise/perlin.hpp +++ b/includes/noise/perlin.hpp @@ -13,7 +13,7 @@ public: private: int seed; void initializeGradients(); - Vector2 grad[512]; + Vector2 grad[256]; Vector2 randoGradient(int ix, int iy); float dotGridGradient(int ix, int iy, float x, float y); float interpolate(float a, float a1, float w); diff --git a/includes/window.hpp b/includes/window.hpp index 9a1b049..754ed80 100644 --- a/includes/window.hpp +++ b/includes/window.hpp @@ -5,6 +5,8 @@ #define HEIGHT 420 int graph(uint32_t *img, bool &needUpdate); +int graph2(uint32_t *img, bool &needUpdate); +int mountain(uint32_t *img, bool &needUpdate); int cloud(uint32_t *img, bool &needUpdate); int marble(uint32_t *img, bool &needUpdate); int random(uint32_t *img, bool &needUpdate); diff --git a/srcs/effects/graph.cpp b/srcs/effects/graph.cpp index 45e2492..9a704a8 100644 --- a/srcs/effects/graph.cpp +++ b/srcs/effects/graph.cpp @@ -1,4 +1,5 @@ #include +#include #include "window.hpp" #include "noise/perlin.hpp" @@ -24,3 +25,26 @@ int graph(uint32_t *img, bool &needUpdate) { return 0; } +int graph2(uint32_t *img, bool &needUpdate) { + if (!needUpdate) + return 0; + needUpdate = false; + static PerlinNoise PerlinNoise; + static float time = 0.0; + for (int x = 0; x < WIDTH; x++) { + float n = PerlinNoise.noise(x / 100.0, time) * 0.5; + + int y = static_cast((n + 1) * (HEIGHT >> 1)); + for (int i = 0; i < HEIGHT; ++i) { + if (i - y < 40 && i - y > -40){ + int co = static_cast(255 * (40 - abs(i - y)) / 40); + img[i * WIDTH + x] = 0 << 16| (co << 8) | co; + } + else + img[i * WIDTH + x] = 0; + } + } + time += 0.001; + needUpdate = true; + return 0; +} diff --git a/srcs/effects/mountain.cpp b/srcs/effects/mountain.cpp new file mode 100644 index 0000000..14f021b --- /dev/null +++ b/srcs/effects/mountain.cpp @@ -0,0 +1,42 @@ +#include +#include +#include "window.hpp" +#include "noise/perlin.hpp" + +int mountain(uint32_t *img, bool &needUpdate) { + if (!needUpdate) + return 0; + needUpdate = false; + static PerlinNoise P(9); + static PerlinNoise P2(1000); + static float time = 0.0; + + /*basic color sunsine gradient no noise*/ + + for (int y = 0; y < HEIGHT; y++) { + for (int x = 0; x < WIDTH; x++) { + float n = P2.noise(x / 10 + (time * 0.5), y / 100.0 + (time * 0.5)) * 0.5 + 0.5; + int color = static_cast(n * 255); +// int color = -y * 255 / HEIGHT; + img[y * WIDTH + x] = (0 << 16) | (color << 8) | color; + } + } + /*mountain*/ + for (int x = 0; x < WIDTH; x++) { + float n = P.noise(x / 100.0 + time, 0) * 0.5; + float n2 = P2.noise(x / 1000.0 + (time), 0) + P2.noise(x / 40.0 + (time * 0.5), 0) * 0.5; + int y = static_cast((n + 1) * (HEIGHT >> 1)); + int y2 = static_cast((n2 + 1) * (HEIGHT >> 1) ); + for (int i = y2; i < HEIGHT; ++i) { + img[i * WIDTH + x] = 0xf0f0f0; + } + for (int i = y; i < HEIGHT; ++i) { + img[i * WIDTH + x] = 0x151515; // Brown color for the mountain + } + } + + + time += 0.03; + needUpdate = true; + return 0; +} diff --git a/srcs/main.cpp b/srcs/main.cpp index 79777a9..f3c59e5 100644 --- a/srcs/main.cpp +++ b/srcs/main.cpp @@ -4,7 +4,9 @@ int main() { - WindowManager window(WIDTH, HEIGHT, graph); + WindowManager window(WIDTH, HEIGHT, mountain); + window.load_render(graph); + window.load_render(graph2); window.load_render(cloud); window.load_render(marble); window.load_render(random); diff --git a/srcs/noise/perlin.cpp b/srcs/noise/perlin.cpp index 1605d17..c6e3457 100644 --- a/srcs/noise/perlin.cpp +++ b/srcs/noise/perlin.cpp @@ -18,7 +18,7 @@ void PerlinNoise::initializeGradients() { std::mt19937 gen(seed); std::uniform_real_distribution dis(0.0, 2 * M_PI); - for (int i = 0; i < 512; ++i) { + for (int i = 0; i < 256; ++i) { float angle = dis(gen); grad[i] = Vector2(cos(angle), sin(angle)); } @@ -26,7 +26,7 @@ void PerlinNoise::initializeGradients() { Vector2 PerlinNoise::randoGradient(int ix, int iy) { uint32_t hash = ix * 7385451 ^ iy * 19349663; - return grad[hash & 512]; + return grad[hash & 0xFF]; } float PerlinNoise::dotGridGradient(int ix, int iy, float x, float y) {