Add nice mountens

This commit is contained in:
Nikola Petrov
2024-03-09 23:00:02 +01:00
parent bb74873557
commit 03f47bcee5
2 changed files with 21 additions and 33 deletions

View File

@@ -3,13 +3,13 @@
#include "raylib.h"
#include "rlgl.h"
#include "raymath.h"
#include "external/stb_perlin.h"
// Public
void BackGround::init(int size)
{
this->size = size;
mountenData.assign(mountenDataCap, 0);
}
void BackGround::newGen()
@@ -17,7 +17,9 @@ void BackGround::newGen()
ClearBackground(backGroundColor);
starts();
moon();
mounten();
mounten(20, 0.3, 550, 650, mountenColor);
mounten(21, 0.3, 650, 730, ColorLerp(mountenColor, BLACK, 0.1f));
mounten(23, 0.3, 730, 780, ColorLerp(mountenColor, BLACK, 0.3f));
}
void BackGround::starts()
@@ -66,58 +68,46 @@ void BackGround::moon()
DrawCircle(xpos, ypos, r, moonColor);
}
void BackGround::mounten()
void BackGround::mounten(size_t mountenSegments, float scale, int min, int max, Color color)
{
int len = 20;
// get random offset in the perlin noise
int offsetX = GetRandomValue(20, 1000);
int offsetY = GetRandomValue(20, 1000);
float sc = (4 / (float)len);
for (size_t i = 0; i < mountenData.size(); i++)
{
float nx = (float)(i + offsetX) * sc;
float p = stb_perlin_fbm_noise3(nx, offsetY, 1.0f, 2.0f, 0.5f, 6);
if (p < -1.0f)
p = -1.0f;
if (p > 1.0f)
p = 1.0f;
// mountenData[i] = (p + 1.0f) / 2.0f;
mountenData[i] = p;
}
drawMounten(600, 100, mountenColor);
}
void BackGround::drawMounten(int line, int varience, Color color)
{
int x = 0;
int diff = size / (mountenData.size() - 1);
int diff = size / (mountenSegments - 1);
float nx = (float)offsetX * scale;
float p = stb_perlin_fbm_noise3(nx, offsetY, 1.0f, 2.0f, 0.5f, 6);
p = (p + 1.0f) / 2.0f;
int y = Lerp(min, max, p);
rlSetTexture(texShapes.id);
rlBegin(RL_QUADS);
rlNormal3f(0.0f, 0.0f, 1.0f);
rlColor4ub(color.r, color.g, color.b, color.a);
int pervY = line + (mountenData[0] * varience);
for (size_t i = 1; i <= mountenData.size(); i++)
for (size_t i = 1; i <= mountenSegments; i++)
{
// topLeft
rlTexCoord2f(0, 0);
rlVertex2f(x, pervY);
rlVertex2f(x, y);
// bottomLeft
rlTexCoord2f(0, 1);
rlVertex2f(x, size);
nx = (float)(i + offsetX) * scale;
p = stb_perlin_fbm_noise3(nx, offsetY, 1.0f, 2.0f, 0.5f, 6);
p = (p + 1.0f) / 2.0f;
y = Lerp(min, max, p);
x += diff;
pervY = line + (mountenData[i] * varience);
// bottomRight
rlTexCoord2f(1, 1);
rlVertex2f(x, size);
// topRight
rlTexCoord2f(1, 0);
rlVertex2f(x, pervY);
rlVertex2f(x, y);
}
rlEnd();
rlSetTexture(0);