211 lines
5.3 KiB
C++
211 lines
5.3 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;
|
|
|
|
constexpr int max_size = 20;
|
|
constexpr int min_size = 2;
|
|
constexpr int max_size_var = 5;
|
|
constexpr int min_size_var = -5;
|
|
constexpr int max_size_chnage = 5;
|
|
constexpr int min_size_change = -5;
|
|
constexpr int sizes[] = {2, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
|
|
static_assert(sizeof(sizes) / sizeof(int) == MAX_POSIBLE_DEPTH);
|
|
|
|
float lengths[MAX_DEPTH];
|
|
|
|
constexpr float max_angles[] = {5.0f, 5.0f, 5.0f, 10.0f, 10.0f, 10.0f, 15.0f, 15.0f, 20.0f, 20.0f, 20.0f};
|
|
static_assert(sizeof(max_angles) / sizeof(float) == MAX_POSIBLE_DEPTH);
|
|
|
|
void calculateLevels(int canvasSize)
|
|
{
|
|
lengths[0] = canvasSize / 4.0f;
|
|
for (size_t i = 1; i < MAX_DEPTH; i++)
|
|
{
|
|
lengths[i] = lengths[i - 1] * 0.7f;
|
|
}
|
|
}
|
|
|
|
// Public
|
|
void Tree::init(int size)
|
|
{
|
|
this->canvasSize = size;
|
|
start.x = size / 2;
|
|
start.y = size;
|
|
calculateLevels(size);
|
|
}
|
|
|
|
void Tree::draw(Dna *dna)
|
|
{
|
|
Circle::setSoftEdge(false);
|
|
|
|
m_dna = dna;
|
|
branchSeed = dna->branchSeed;
|
|
draw_calls.push_back({start, 180.0f, 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();
|
|
if (arg.dep == MAX_DEPTH)
|
|
return;
|
|
float angle_var = get_angle_var(arg);
|
|
float angle = ((arg.angleDeg + angle_var) * PI) / 180.0f;
|
|
float length = get_lenght(arg);
|
|
float nx = length * std::sin(angle);
|
|
float ny = length * std::cos(angle);
|
|
Vector2 end = {arg.start.x + nx, arg.start.y + ny};
|
|
|
|
int size_start = get_start_size(arg);
|
|
int size_end = get_end_size(arg, size_start);
|
|
float fstep = 1.0 / ((length / size_start) * 2.0f);
|
|
|
|
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);
|
|
int size = Lerp(size_start, size_end, i);
|
|
DrawCircleV(point, size, 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 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.dep + 1, colorEnd, size_end});
|
|
}
|
|
}
|
|
|
|
inline int Tree::get_num_of_branches(int 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(int 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};
|
|
}
|
|
|
|
inline int Tree::get_start_size(DrawArgs &arg)
|
|
{
|
|
int size = Remap(m_dna->branches[arg.dep].size, 0, 255, min_size, max_size);
|
|
size += Remap(m_dna->branches[arg.dep].size_var, 0, 255, min_size_var, max_size_var) * mrand::getFloat(&branchSeed);
|
|
|
|
if (arg.dep > 0)
|
|
{
|
|
float size_parent = m_dna->branches[arg.dep].size_parent / 255.0f;
|
|
size = std::lerp(size, arg.size, size_parent);
|
|
}
|
|
|
|
float mix_level = m_dna->branches[arg.dep].size_level / 255.0f;
|
|
size = std::lerp(size, sizes[MAX_DEPTH - arg.dep - 1], mix_level);
|
|
|
|
if (size < 1)
|
|
size = 1;
|
|
return size;
|
|
}
|
|
|
|
inline int Tree::get_end_size(DrawArgs &arg, int start)
|
|
{
|
|
int size = Remap(m_dna->branches[arg.dep].size_change, 0, 255, min_size_change, max_size_chnage);
|
|
size += start;
|
|
|
|
if (size < 1)
|
|
size = 1;
|
|
return size;
|
|
}
|
|
|
|
inline float Tree::get_lenght(DrawArgs &arg)
|
|
{
|
|
float lenght = lengths[arg.dep];
|
|
float lenght_ratio = Remap(m_dna->branches[arg.dep].length, 0, 255, 0.5f, 1.3f);
|
|
lenght *= lenght_ratio;
|
|
float lenght_var = Remap(m_dna->branches[arg.dep].length_var, 0, 255, -0.15f, 0.15f);
|
|
lenght += lenght * lenght_var * mrand::getFloat(&branchSeed);
|
|
if (lenght < 1)
|
|
lenght = 1;
|
|
return lenght;
|
|
}
|
|
|
|
inline float Tree::get_angle_var(DrawArgs &arg)
|
|
{
|
|
float angle_var = Remap(m_dna->branches[arg.dep].branch_angle_var, 0, 255, 0.0f, max_angles[arg.dep]);
|
|
|
|
angle_var = Lerp(angle_var, -angle_var, mrand::getFloat(&branchSeed));
|
|
|
|
return angle_var;
|
|
}
|