Add DNA to tree

This commit is contained in:
Nikola Petrov 2024-12-21 00:20:43 +01:00
parent ee7b674c6d
commit 2a215ead5f
6 changed files with 57 additions and 57 deletions

View File

@ -10,9 +10,11 @@ public:
~Canvas() = default;
void init(int size);
void deinit();
void newGen(RenderTexture2D& target);
void newGen(RenderTexture2D &target);
private:
BackGround backGround;
Tree tree;
Dna dna = {0};
};

View File

@ -3,14 +3,7 @@
#include <list>
#include <raylib.h>
#define MAX_DEPTH 11
struct Branch
{
Color color;
uint8_t numOfBranches;
float lenghthRatio;
};
#include "values/Dna.hpp"
struct DrawArgs
{
@ -27,16 +20,16 @@ public:
Tree() = default;
~Tree() = default;
void init(int size);
void newGen();
void draw(Dna *dna);
private:
Dna *m_dna;
int size = 0;
Vector2 start = {0};
std::vector<Branch> branches;
std::list<DrawArgs> draw_calls;
void generateBranches();
void drawTree();
void drawBranch();
Vector2 drawLine();
};

View File

@ -1,4 +1,12 @@
#ifndef DNA_H
#define DNA_H
#include <cinttypes>
#include <array>
#include <raylib.h>
#define MAX_DEPTH 11
struct uint128
{
@ -15,6 +23,13 @@ struct Moon
float size;
};
struct Branch
{
Color color;
uint8_t numOfBranches;
float lenghthRatio;
};
struct Dna
{
Moon moon;
@ -22,6 +37,9 @@ struct Dna
int time;
uint128 mountenSeed;
uint128 starSeed;
std::array<Branch, MAX_DEPTH> branches;
};
Dna newDna();
void newDna(Dna &dna);
#endif /* DNA_H */

View File

@ -11,10 +11,10 @@ void Canvas::newGen(RenderTexture2D &target)
BeginTextureMode(target);
ClearBackground(WHITE);
Dna dna = newDna();
newDna(dna);
backGround.draw(&dna);
tree.newGen();
tree.draw(&dna);
EndTextureMode();
}

View File

@ -3,7 +3,6 @@
#include <raymath.h>
#include "canvas/Tree.hpp"
#include "Math.hpp"
#include "values/mrand.hpp"
// Public
void Tree::init(int size)
@ -11,15 +10,18 @@ void Tree::init(int size)
this->size = size;
start.x = size / 2;
start.y = size;
branches.assign(11, {});
}
void Tree::newGen()
void Tree::draw(Dna *dna)
{
generateBranches();
m_dna = dna;
draw_calls.push_back({start, 0, (float)size / 4, 1});
drawTree();
while (!draw_calls.empty())
{
drawBranch();
draw_calls.pop_front();
}
}
// Private
@ -40,7 +42,7 @@ Vector2 Tree::drawLine()
for (float i = 0; i < 1; i += fstep)
{
Vector2 point = Vector2Lerp(arg.start, end, i);
Color color = ColorLerp(branches[arg.dep - 1].color, branches[arg.dep].color, i);
Color color = ColorLerp(m_dna->branches[arg.dep - 1].color, m_dna->branches[arg.dep].color, i);
DrawCircleV(point, thick, color);
}
return end;
@ -54,41 +56,13 @@ void Tree::drawBranch()
Vector2 next = drawLine();
float next_len = branches[arg.dep].lenghthRatio;
float sectors = branches[arg.dep].numOfBranches + 1;
float next_len = m_dna->branches[arg.dep].lenghthRatio;
float sectors = m_dna->branches[arg.dep].numOfBranches + 1;
float degres = 180.0f / sectors;
for (size_t i = 0; i < branches[arg.dep].numOfBranches; i++)
for (size_t i = 0; i < m_dna->branches[arg.dep].numOfBranches; i++)
{
float newAngle = arg.angleDeg - 90 + (degres * (i + 1));
draw_calls.push_back({next, newAngle, arg.lenghth * next_len, arg.dep + 1});
}
}
void Tree::drawTree()
{
draw_calls.push_back({start, 0, (float)size / 4, 1});
while (!draw_calls.empty())
{
drawBranch();
draw_calls.pop_front();
}
}
void Tree::generateBranches()
{
for (size_t i = 0; i < MAX_DEPTH; i++)
{
uint8_t r = mrand::getValue(0, 255);
uint8_t g = mrand::getValue(0, 255);
uint8_t b = mrand::getValue(0, 255);
branches[i].color = {r, g, b, 255};
branches[i].numOfBranches = mrand::getValue(1, 3);
branches[i].lenghthRatio = ((float)mrand::getValue(600, 700)) / 1000.0f;
}
branches[0].color = branches[1].color;
}

View File

@ -5,9 +5,8 @@
#include <raymath.h>
Dna newDna()
void newDna(Dna &dna)
{
Dna dna = {0};
dna.moon = {mrand::getFloat(), mrand::getFloat(), mrand::getFloat()};
dna.colorSet = mrand::getValue(0, 3);
dna.time = std::floor(Remap(mrand::getFloat(), 0, 1, 4, 0));
@ -21,5 +20,19 @@ Dna newDna()
dna.starSeed.c = mrand::getInt();
dna.starSeed.d = mrand::getInt();
return dna;
for (size_t i = 0; i < MAX_DEPTH; i++)
{
uint8_t r = mrand::getValue(0, 255);
uint8_t g = mrand::getValue(0, 255);
uint8_t b = mrand::getValue(0, 255);
dna.branches[i].color = {r, g, b, 255};
dna.branches[i].numOfBranches = mrand::getValue(1, 3);
dna.branches[i].lenghthRatio = ((float)mrand::getValue(600, 700)) / 1000.0f;
}
dna.branches[0].color = dna.branches[1].color;
return;
}