-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfractal.h
More file actions
103 lines (81 loc) · 3.4 KB
/
Copy pathfractal.h
File metadata and controls
103 lines (81 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef FRACTAL_TREE_H
#define FRACTAL_TREE_H
#include <glm/glm.hpp>
#include <vector>
#include "Shader.h"
class FractalTree {
public:
glm::vec3 branchColor; // Color of the branches
float branchWidth; // Width of the branches
FractalTree(float branchLength = 1.0f, float branchAngle = 30.0f, int recursionDepth = 10,
glm::vec3 color = glm::vec3(0.22f, 1.0f, 0.078f), float width = 5.0f)
{
this->branchLength = branchLength;
this->branchAngle = branchAngle;
this->recursionDepth = recursionDepth;
this->branchColor = color;
this->branchWidth = width;
buildTree();
// Generate VAO and VBO for rendering
glGenVertexArrays(1, &treeVAO);
glBindVertexArray(treeVAO);
glGenBuffers(1, &treeVBO);
glBindBuffer(GL_ARRAY_BUFFER, treeVBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW);
// Enable vertex attribute for position
glEnableVertexAttribArray(0); // Position
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
// Unbind VAO and VBO
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
~FractalTree() {
glDeleteVertexArrays(1, &treeVAO);
glDeleteBuffers(1, &treeVBO);
}
void drawTree(Shader& shader, glm::mat4 model) const {
shader.use();
shader.setVec3("color", branchColor);
shader.setMat4("model", model);
// Set line width for branches
glLineWidth(branchWidth);
glBindVertexArray(treeVAO);
glDrawArrays(GL_LINES, 0, vertices.size() / 3);
glBindVertexArray(0);
// Reset line width to default for other drawings
glLineWidth(1.0f);
}
private:
unsigned int treeVAO, treeVBO;
float branchLength; // Length of the branches
float branchAngle; // Angle between branches
int recursionDepth; // Maximum depth of recursion
std::vector<float> vertices; // Stores the tree's vertices
void buildTree() {
glm::vec3 start(0.0f, 0.0f, 0.0f); // Start at origin
glm::vec3 direction(0.0f, branchLength, 0.0f); // Initial upward direction
generateBranches(start, direction, recursionDepth);
}
void generateBranches(const glm::vec3& start, const glm::vec3& direction, int depth) {
if (depth == 0) return;
// Compute the end point of the branch
glm::vec3 end = start + direction;
// Add the branch to the vertices list
vertices.push_back(start.x);
vertices.push_back(start.y);
vertices.push_back(start.z);
vertices.push_back(end.x);
vertices.push_back(end.y);
vertices.push_back(end.z);
// Compute new branch directions
float angleRadians = glm::radians(branchAngle);
glm::mat4 rotationLeft = glm::rotate(glm::mat4(1.0f), angleRadians, glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 rotationRight = glm::rotate(glm::mat4(1.0f), -angleRadians, glm::vec3(0.0f, 0.0f, 1.0f));
glm::vec3 leftDirection = glm::vec3(rotationLeft * glm::vec4(direction * 0.7f, 0.0f));
glm::vec3 rightDirection = glm::vec3(rotationRight * glm::vec4(direction * 0.7f, 0.0f));
// Recursively generate branches
generateBranches(end, leftDirection, depth - 1);
generateBranches(end, rightDirection, depth - 1);
}
};
#endif // FRACTAL_TREE_H