2024-12-30 21:53:13 +01:00

133 lines
3.0 KiB
C++

#include <cmath>
#include "canvas/Tree.hpp"
#include "canvas/Circle.hpp"
#include "values/mrand.hpp"
#include "Math.hpp"
#include <raylib.h>
#include <raymath.h>
#define ITER_PER_FRAME 5000
constexpr int max_num_of_branches = 3;
constexpr int max_color_change = 15;
constexpr int min_color_change = -15;
constexpr float color_parent_mix = 0.6f;
// Public
void Tree::init(int size)
{
this->size = size;
start.x = size / 2;
start.y = size;
}
void Tree::draw(Dna *dna)
{
Circle::setSoftEdge(false);
m_dna = dna;
branchSeed = dna->branchSeed;
draw_calls.push_back({start, 0, (float)size / 4, 0});
tick();
}
bool Tree::tick()
{
size_t i = 0;
while (!draw_calls.empty())
{
drawBranch();
draw_calls.pop_front();
i++;
if (i >= ITER_PER_FRAME)
break;
}
return draw_calls.empty();
}
// Private
void Tree::drawBranch()
{
DrawArgs arg = draw_calls.front();
float angle = ((arg.angleDeg + 180.0f) * PI) / 180.0f;
float nx = arg.lenghth * std::sin(angle);
float ny = arg.lenghth * std::cos(angle);
Vector2 end = {arg.start.x + nx, arg.start.y + ny};
float thick = 2.0;
float fstep = 1.0 / ((arg.lenghth / thick) * 1.5);
Color colorStart = get_start_color(arg);
Color colorEnd = get_end_color(arg.dep, colorStart);
for (float i = 0; i < 1; i += fstep)
{
Vector2 point = Vector2Lerp(arg.start, end, i);
Color color = ColorLerp(colorStart, colorEnd, i);
DrawCircleV(point, thick, color); // Fester on the phone to call DrawCircle insted of the Circle shader
// Circle::setColor(color);
// Circle::draw(point.x, point.y, thick); // TODO Change to BeginShaderMode and EndShaderMode only onece
// use
// DrawRectangleGradientEx
}
// add more branches to draw
if (arg.dep + 1 >= MAX_DEPTH)
return;
float next_len = 0.7f;
float sectors = get_num_of_branches(arg.dep) + 1;
float degres = 180.0f / sectors;
for (size_t i = 0; i < get_num_of_branches(arg.dep); i++)
{
float newAngle = arg.angleDeg - 90 + (degres * (i + 1));
draw_calls.push_back({end, newAngle, arg.lenghth * next_len, arg.dep + 1, colorEnd});
}
}
inline uint8_t Tree::get_num_of_branches(uint8_t dep)
{
if (m_dna->branches[dep].branch_count < 128)
return 2;
else
return 3;
}
inline Color Tree::get_start_color(DrawArgs &arg)
{
Color ret = {
m_dna->branches[arg.dep].colorR,
m_dna->branches[arg.dep].colorG,
m_dna->branches[arg.dep].colorB,
255};
if (arg.dep > 0)
{
ret = ColorLerp(ret, arg.parent, color_parent_mix);
}
int color_var = Remap(m_dna->branches[arg.dep].color_var, 0, 255, min_color_change, max_color_change);
ret.r += color_var * mrand::getFloat(&branchSeed);
ret.g += color_var * mrand::getFloat(&branchSeed);
ret.b += color_var * mrand::getFloat(&branchSeed);
return ret;
}
inline Color Tree::get_end_color(uint8_t dep, Color &start)
{
return {
start.r + m_dna->branches[dep].colorR_change,
start.g + m_dna->branches[dep].colorG_change,
start.b + m_dna->branches[dep].colorB_change,
255};
}