tree.cpp 2.38 KiB
#include "tree.hpp"
#include <math.h>
using namespace cgp;
mesh create_cylinder_mesh(float radius, float height)
{
mesh m;
int n = 1000;
for (int i = 0; i < n; i++) {
m.position.push_back(vec3{ cos(2 * Pi * i / n) * radius, sin(2 * Pi * i / n) * radius, (i % 2) * height- 0.05f * ((i+1) % 2) });
}
for (int i = 0; i < n; i++) {
if (i % 2 == 0) m.connectivity.push_back(uint3{ i, (i+2)%n, (i + 1) % n });
else m.connectivity.push_back(uint3{i, (i + 1) % n, (i+2)%n});
}
// To do: fill this mesh ...
// ...
// To add a position:
// m.position.push_back(vec3{x,y,z})
// Or in pre-allocating the buffer:
// m.position.resize(maximalSize);
// m.position[index] = vec3{x,y,z}; (with 0<= index < maximalSize)
//
// Similar with the triangle connectivity:
// m.connectivity.push_back(uint3{index_1, index_2, index_3});
// Need to call fill_empty_field() before returning the mesh
// this function fill all empty buffer with default values (ex. normals, colors, etc).
m.fill_empty_field();
return m;
}
mesh create_cone_mesh(float radius, float height, float z_offset)
{
mesh m;
int n = 10;
for (int i = 0; i < n; i++) {
m.position.push_back(vec3{ cos(2 * Pi * i / n) * radius, sin(2 * Pi * i / n) * radius, z_offset});
}
m.position.push_back(vec3{0, 0, z_offset });
m.position.push_back(vec3{0, 0, z_offset + height });
for (int i = 0; i < n; i++) {
m.connectivity.push_back(uint3(i, n, (i + 1) % n));
m.connectivity.push_back(uint3(i, (i + 1) % n, n + 1));
}
m.fill_empty_field();
return m;
}
mesh create_tree()
{
float h = 0.7f; // trunk height
float r = 0.1f; // trunk radius
// Create a brown trunk
mesh trunk = create_cylinder_mesh(r, h);
trunk.color.fill({ 0.4f, 0.3f, 0.3f });
// Create a green foliage from 3 cones
mesh foliage = create_cone_mesh(4 * r, 6 * r, 0.0f); // base-cone
foliage.push_back(create_cone_mesh(4 * r, 6 * r, 2 * r)); // middle-cone
foliage.push_back(create_cone_mesh(4 * r, 6 * r, 4 * r)); // top-cone
foliage.apply_translation_to_position({ 0,0,h }); // place foliage at the top of the trunk
foliage.color.fill({ 0.4f, 0.6f, 0.3f });
// The tree is composed of the trunk and the foliage
mesh tree = trunk;
tree.push_back(foliage);
return tree;
}