diplomska_naloga/main.cpp
Nikola Petrov 3d9e6c9f7d change to draw body first
Using std::list to store what needs to be drawn
2024-02-22 13:33:10 +01:00

130 lines
2.8 KiB
C++

#include <cmath>
#include <vector>
#include <cstdint>
#include <list>
#include "raylib.h"
#include "raymath.h"
#define MAX_DEPTH 11
Color ColorLerp(Color c1, Color c2, float amount)
{
Color ret{0};
ret.r = Clamp(Lerp(c1.r, c2.r, amount), 0, 255);
ret.g = Clamp(Lerp(c1.g, c2.g, amount), 0, 255);
ret.b = Clamp(Lerp(c1.b, c2.b, amount), 0, 255);
ret.a = Clamp(Lerp(c1.a, c2.a, amount), 0, 255);
return ret;
}
struct branch
{
Color color;
};
struct draw_args
{
Vector2 start;
float angleDeg;
float lenghth;
int dep;
};
static std::vector<branch> tree(MAX_DEPTH);
static std::list<draw_args> draw_calls;
Vector2 draw_line(Vector2 start, float angleDeg, float lenghth, int dep)
{
angleDeg += 180.0f;
float angle = (angleDeg * PI) / 180.0f;
float nx = lenghth * std::sin(angle);
float ny = lenghth * std::cos(angle);
Vector2 end = {start.x + nx, start.y + ny};
float thick = 2.0;
float fstep = 1.0 / ((lenghth / thick) * 1.5);
for (float i = 0; i < 1; i += fstep)
{
Vector2 point = Vector2Lerp(start, end, i);
Color color = ColorLerp(tree[dep - 1].color, tree[dep].color, i);
DrawCircleV(point, thick, color);
}
return end;
}
void draw_branch(draw_args arg)
{
if (arg.dep >= MAX_DEPTH)
return;
Vector2 next = draw_line(arg.start, arg.angleDeg, arg.lenghth, arg.dep);
float next_len = 0.7f;
draw_calls.push_back((draw_args){next, arg.angleDeg + 45, arg.lenghth * next_len, arg.dep + 1});
draw_calls.push_back((draw_args){next, arg.angleDeg - 45, arg.lenghth * next_len, arg.dep + 1});
}
void draw_tree(draw_args first)
{
draw_calls.push_back(first);
while (!draw_calls.empty())
{
draw_branch(draw_calls.front());
draw_calls.pop_front();
}
}
void generate_tree()
{
for (size_t i = 0; i < MAX_DEPTH; i++)
{
uint8_t r = GetRandomValue(0, 255);
uint8_t g = GetRandomValue(0, 255);
uint8_t b = GetRandomValue(0, 255);
tree[i].color = (Color){r, g, b, 255};
}
tree[0].color = tree[1].color;
}
int main(void)
{
const int screenWidth = 800;
const int screenHeight = 800;
InitWindow(screenWidth, screenHeight, "raylib");
SetTargetFPS(60);
const int textureWidth = screenWidth;
const int textureHeight = screenHeight;
RenderTexture2D target = LoadRenderTexture(textureWidth, textureHeight);
Vector2 start = {textureWidth / 2, textureHeight};
generate_tree();
BeginTextureMode(target);
ClearBackground(WHITE);
draw_tree((draw_args){start, 0, textureHeight / 4, 1});
EndTextureMode();
Rectangle source = {0, 0, (float)target.texture.width, (float)-target.texture.height};
Rectangle dest = {0, 0, screenWidth, screenHeight};
Vector2 position = {0, 0};
Vector2 origin = {0.0f, 0.0f};
while (!WindowShouldClose())
{
BeginDrawing();
DrawTexturePro(target.texture, source, dest, origin, 0.0f, WHITE);
EndDrawing();
}
CloseWindow();
return 0;
}