Use Dna color

This commit is contained in:
Nikola Petrov 2024-12-30 21:53:13 +01:00
parent 46a739ae28
commit 5670183dc0
3 changed files with 30 additions and 31 deletions

View File

@ -11,6 +11,7 @@ struct DrawArgs
float angleDeg;
float lenghth;
int dep;
Color parent;
};
class Tree
@ -32,9 +33,8 @@ private:
std::list<DrawArgs> draw_calls;
void drawBranch();
Vector2 drawLine();
inline uint8_t get_num_of_branches(uint8_t dep);
inline Color get_start_color(uint8_t dep);
inline Color get_start_color(DrawArgs &arg);
inline Color get_end_color(uint8_t dep, Color &start);
};

View File

@ -6,7 +6,7 @@
#include <raylib.h>
#define MAX_DEPTH 10
#define MAX_DEPTH 8
struct uint128
{
@ -31,7 +31,6 @@ struct Branch
int8_t colorR_change;
int8_t colorG_change;
int8_t colorB_change;
uint8_t color_parent;
uint8_t color_var;
uint8_t size;

View File

@ -2,6 +2,7 @@
#include "canvas/Tree.hpp"
#include "canvas/Circle.hpp"
#include "values/mrand.hpp"
#include "Math.hpp"
#include <raylib.h>
@ -10,6 +11,9 @@
#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)
@ -25,7 +29,7 @@ void Tree::draw(Dna *dna)
m_dna = dna;
branchSeed = dna->branchSeed;
draw_calls.push_back({start, 0, (float)size / 4, 1});
draw_calls.push_back({start, 0, (float)size / 4, 0});
tick();
}
@ -46,12 +50,11 @@ bool Tree::tick()
// Private
Vector2 Tree::drawLine()
void Tree::drawBranch()
{
DrawArgs arg = draw_calls.front();
arg.angleDeg += 180.0f;
float angle = (arg.angleDeg * PI) / 180.0f;
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};
@ -59,7 +62,7 @@ Vector2 Tree::drawLine()
float thick = 2.0;
float fstep = 1.0 / ((arg.lenghth / thick) * 1.5);
Color colorStart = get_start_color(arg.dep);
Color colorStart = get_start_color(arg);
Color colorEnd = get_end_color(arg.dep, colorStart);
@ -74,17 +77,12 @@ Vector2 Tree::drawLine()
// use
// DrawRectangleGradientEx
}
return end;
}
void Tree::drawBranch()
{
DrawArgs arg = draw_calls.front();
if (arg.dep >= MAX_DEPTH)
// add more branches to draw
if (arg.dep + 1 >= MAX_DEPTH)
return;
Vector2 next = drawLine();
float next_len = 0.7f;
float sectors = get_num_of_branches(arg.dep) + 1;
float degres = 180.0f / sectors;
@ -92,34 +90,36 @@ void Tree::drawBranch()
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({next, newAngle, arg.lenghth * next_len, arg.dep + 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)
{
return ((float)m_dna->branches[dep].branch_count / 255.0f) * max_num_of_branches + 1;
if (m_dna->branches[dep].branch_count < 128)
return 2;
else
return 3;
}
inline Color Tree::get_start_color(uint8_t dep)
inline Color Tree::get_start_color(DrawArgs &arg)
{
Color ret = {
m_dna->branches[dep].colorR,
m_dna->branches[dep].colorG,
m_dna->branches[dep].colorB,
m_dna->branches[arg.dep].colorR,
m_dna->branches[arg.dep].colorG,
m_dna->branches[arg.dep].colorB,
255};
if (dep > 0)
if (arg.dep > 0)
{
Color parent = {
m_dna->branches[dep - 1].colorR,
m_dna->branches[dep - 1].colorG,
m_dna->branches[dep - 1].colorB,
255};
float mix = ((float)m_dna->branches[dep].color_parent) / 255.0f;
ret = ColorLerp(ret, parent, mix);
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;
}