add graph2 and mountaine

This commit is contained in:
loic 2024-12-14 03:07:49 +01:00
parent 8a0c1840b8
commit c7ac26c559
6 changed files with 74 additions and 4 deletions

View file

@ -13,7 +13,7 @@ public:
private: private:
int seed; int seed;
void initializeGradients(); void initializeGradients();
Vector2 grad[512]; Vector2 grad[256];
Vector2 randoGradient(int ix, int iy); Vector2 randoGradient(int ix, int iy);
float dotGridGradient(int ix, int iy, float x, float y); float dotGridGradient(int ix, int iy, float x, float y);
float interpolate(float a, float a1, float w); float interpolate(float a, float a1, float w);

View file

@ -5,6 +5,8 @@
#define HEIGHT 420 #define HEIGHT 420
int graph(uint32_t *img, bool &needUpdate); 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 cloud(uint32_t *img, bool &needUpdate);
int marble(uint32_t *img, bool &needUpdate); int marble(uint32_t *img, bool &needUpdate);
int random(uint32_t *img, bool &needUpdate); int random(uint32_t *img, bool &needUpdate);

View file

@ -1,4 +1,5 @@
#include <cstdint> #include <cstdint>
#include <cmath>
#include "window.hpp" #include "window.hpp"
#include "noise/perlin.hpp" #include "noise/perlin.hpp"
@ -24,3 +25,26 @@ int graph(uint32_t *img, bool &needUpdate) {
return 0; 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<int>((n + 1) * (HEIGHT >> 1));
for (int i = 0; i < HEIGHT; ++i) {
if (i - y < 40 && i - y > -40){
int co = static_cast<int>(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;
}

42
srcs/effects/mountain.cpp Normal file
View file

@ -0,0 +1,42 @@
#include <cstdint>
#include <cmath>
#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<int>(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<int>((n + 1) * (HEIGHT >> 1));
int y2 = static_cast<int>((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;
}

View file

@ -4,7 +4,9 @@
int main() { 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(cloud);
window.load_render(marble); window.load_render(marble);
window.load_render(random); window.load_render(random);

View file

@ -18,7 +18,7 @@ void PerlinNoise::initializeGradients() {
std::mt19937 gen(seed); std::mt19937 gen(seed);
std::uniform_real_distribution<float> dis(0.0, 2 * M_PI); std::uniform_real_distribution<float> dis(0.0, 2 * M_PI);
for (int i = 0; i < 512; ++i) { for (int i = 0; i < 256; ++i) {
float angle = dis(gen); float angle = dis(gen);
grad[i] = Vector2(cos(angle), sin(angle)); grad[i] = Vector2(cos(angle), sin(angle));
} }
@ -26,7 +26,7 @@ void PerlinNoise::initializeGradients() {
Vector2 PerlinNoise::randoGradient(int ix, int iy) { Vector2 PerlinNoise::randoGradient(int ix, int iy) {
uint32_t hash = ix * 7385451 ^ iy * 19349663; 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) { float PerlinNoise::dotGridGradient(int ix, int iy, float x, float y) {