Files
treender/shared/src/canvas/Tree.cpp
2025-12-04 13:31:30 +01:00

248 lines
7.0 KiB
C++

#include <cmath>
#include "canvas/Tree.hpp"
#include <raylib.h>
#include <raymath.h>
#include <rlgl.h>
#define ITER_PER_FRAME 5000
constexpr int maxColorChange = 15;
constexpr int minColorChange = -15;
constexpr float colorParentMix = 0.6f;
constexpr int maxSize = 20;
constexpr int minSize = 2;
constexpr int maxSizeVar = 5;
constexpr int minSizeVar = -5;
constexpr int maxSizeChange = 5;
constexpr int MinSizeChange = -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 maxAngles[] = {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(maxAngles) / 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)
{
start.x = size / 2;
start.y = size;
calculateLevels(size);
// texBunny = LoadTexture("dot.png"); // bug add deinit to unload texutre
}
void Tree::draw(Dna *dna)
{
m_dna = dna;
branchSeed = dna->branchSeed;
drawCalls.push_back({start, 180.0f, 0});
tick();
}
bool Tree::tick()
{
size_t i = 0;
while (!drawCalls.empty())
{
calculateBranch();
drawCalls.pop_front();
i++;
if (i >= ITER_PER_FRAME)
break;
}
return drawCalls.empty();
}
// Private
void Tree::calculateBranch()
{
DrawArgs arg = drawCalls.front();
if (arg.dep == MAX_DEPTH)
return;
float angleVar = getAngleVar(arg);
float angle = ((arg.angleDeg + angleVar) * PI) / 180.0f;
float length = getLength(arg);
float nx = length * std::sin(angle);
float ny = length * std::cos(angle);
Vector2 end = {arg.start.x + nx, arg.start.y + ny};
int sizeStart = getStartSize(arg);
int sizeEnd = getEndSize(arg, sizeStart);
float fstep = 1.0 / ((length / sizeStart) * 2.0f);
Color colorStart = getStartColor(arg);
Color colorEnd = getEndColor(arg.dep, colorStart);
// drawBranch(arg.start, end, colorStart, colorEnd, sizeStart, sizeEnd);
for (float i = 0; i < 1; i += fstep)
{
Vector2 point = Vector2Lerp(arg.start, end, i);
Color color = ColorLerp(colorStart, colorEnd, i);
int size = Lerp(sizeStart, sizeEnd, i);
DrawCircleV(point, size, color);
// DrawTextureEx(texBunny, point,0, ((float)size) / texBunny.height, color);
}
// add more branches to draw
if (arg.dep + 1 >= MAX_DEPTH)
return;
float sectors = getNumOfBranches(arg.dep) + 1;
float degres = 180.0f / sectors;
for (size_t i = 0; i < getNumOfBranches(arg.dep); i++)
{
float newAngle = arg.angleDeg - 90 + (degres * (i + 1));
drawCalls.push_back({end, newAngle, arg.dep + 1, colorEnd, sizeEnd});
}
}
inline size_t Tree::getNumOfBranches(int dep)
{
if (m_dna->branches[dep].branchCount < 128)
return 2;
else
return 3;
}
inline Color Tree::getStartColor(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, colorParentMix);
}
int colorVar = Remap(m_dna->branches[arg.dep].colorVar, 0, 255, minColorChange, maxColorChange);
ret.r += colorVar * mrand::getFloat(&branchSeed);
ret.g += colorVar * mrand::getFloat(&branchSeed);
ret.b += colorVar * mrand::getFloat(&branchSeed);
return ret;
}
inline Color Tree::getEndColor(int dep, Color &start)
{
uint8_t r = start.r + m_dna->branches[dep].colorR_change;
uint8_t g = start.g + m_dna->branches[dep].colorG_change;
uint8_t b = start.b + m_dna->branches[dep].colorB_change;
return {r, g, b, 255};
}
inline int Tree::getStartSize(DrawArgs &arg)
{
int size = Remap(m_dna->branches[arg.dep].size, 0, 255, minSize, maxSize);
size += Remap(m_dna->branches[arg.dep].sizeVar, 0, 255, minSizeVar, maxSizeVar) * mrand::getFloat(&branchSeed);
if (arg.dep > 0)
{
float sizeParent = m_dna->branches[arg.dep].sizeParent / 255.0f;
size = std::lerp(size, arg.size, sizeParent);
}
float mixLevel = m_dna->branches[arg.dep].sizeLevel / 255.0f;
size = std::lerp(size, sizes[MAX_DEPTH - arg.dep - 1], mixLevel);
if (size < 1)
size = 1;
return size;
}
inline int Tree::getEndSize(DrawArgs &arg, int start)
{
int size = Remap(m_dna->branches[arg.dep].sizeChange, 0, 255, MinSizeChange, maxSizeChange);
size += start;
if (size < 1)
size = 1;
return size;
}
inline float Tree::getLength(DrawArgs &arg)
{
float lenght = lengths[arg.dep];
float lenghtRatio = Remap(m_dna->branches[arg.dep].length, 0, 255, 0.5f, 1.3f);
lenght *= lenghtRatio;
float lenghtVar = Remap(m_dna->branches[arg.dep].lengthVar, 0, 255, -0.15f, 0.15f);
lenght += lenght * lenghtVar * mrand::getFloat(&branchSeed);
if (lenght < 1)
lenght = 1;
return lenght;
}
inline float Tree::getAngleVar(DrawArgs &arg)
{
float angleVar = Remap(m_dna->branches[arg.dep].branchAngleVar, 0, 255, 0.0f, maxAngles[arg.dep]);
angleVar = Lerp(angleVar, -angleVar, mrand::getFloat(&branchSeed));
return angleVar;
}
void Tree::drawBranch(Vector2 startPoint, Vector2 endPoint, Color startColor, Color endColor, float startThickness, float endThickness)
{
// Calculate the direction vector from startPoint to endPoint
Vector2 direction = {endPoint.x - startPoint.x, endPoint.y - startPoint.y};
// Normalize the direction vector
float length = sqrtf(direction.x * direction.x + direction.y * direction.y);
if (length == 0)
length = 1; // Avoid division by zero
Vector2 normalizedDir = {direction.x / length, direction.y / length};
// Calculate the perpendicular vector (rotate 90 degrees)
Vector2 perpendicular = {-normalizedDir.y, normalizedDir.x};
// Calculate the four vertices of the quadrilateral
Vector2 topLeft = {
startPoint.x + perpendicular.x * startThickness,
startPoint.y + perpendicular.y * startThickness};
Vector2 topRight = {
endPoint.x + perpendicular.x * endThickness,
endPoint.y + perpendicular.y * endThickness};
Vector2 bottomLeft = {
startPoint.x - perpendicular.x * startThickness,
startPoint.y - perpendicular.y * startThickness};
Vector2 bottomRight = {
endPoint.x - perpendicular.x * endThickness,
endPoint.y - perpendicular.y * endThickness};
// Draw the two triangles to form the quadrilateral
rlBegin(RL_TRIANGLES);
// First triangle
rlColor4ub(startColor.r, startColor.g, startColor.b, startColor.a);
rlVertex2f(topLeft.x, topLeft.y);
rlColor4ub(endColor.r, endColor.g, endColor.b, endColor.a);
rlVertex2f(topRight.x, topRight.y);
rlColor4ub(startColor.r, startColor.g, startColor.b, startColor.a);
rlVertex2f(bottomLeft.x, bottomLeft.y);
// Second triangle
rlColor4ub(startColor.r, startColor.g, startColor.b, startColor.a);
rlVertex2f(bottomLeft.x, bottomLeft.y);
rlColor4ub(endColor.r, endColor.g, endColor.b, endColor.a);
rlVertex2f(topRight.x, topRight.y);
rlColor4ub(endColor.r, endColor.g, endColor.b, endColor.a);
rlVertex2f(bottomRight.x, bottomRight.y);
rlEnd();
}