14constexpr float kSunk = 0.15f;
16Color ColorAt(
const Image& texture,
float u,
float v) {
18 std::clamp(
static_cast<int>(u * texture.width), 0, texture.width - 1);
20 std::clamp(
static_cast<int>(v * texture.height), 0, texture.height - 1);
21 return GetImageColor(texture, x, y);
27 return std::string(GetApplicationDirectory()) +
"trees";
30std::vector<Face>
LoadFaces(
const std::string& path) {
31 if (!FileExists(path.c_str())) {
34 Model model = LoadModel(path.c_str());
35 const BoundingBox box = GetModelBoundingBox(model);
36 const float scale = 1.0f / (box.max.y - box.min.y);
37 std::vector<Face> faces;
38 for (
int m = 0; m < model.meshCount; ++m) {
39 const Mesh& mesh = model.meshes[m];
40 const Material& material = model.materials[model.meshMaterial[m]];
43 LoadImageFromTexture(material.maps[MATERIAL_MAP_DIFFUSE].texture);
44 const auto corner = [&](
int i) {
45 return Vector3{mesh.vertices[3 * i] * scale,
46 (mesh.vertices[3 * i + 1] - box.min.y) * scale,
47 mesh.vertices[3 * i + 2] * scale};
49 for (
int t = 0; t < mesh.triangleCount; ++t) {
53 for (
int k = 0; k < 3; ++k) {
54 corners[k] = mesh.indices ? mesh.indices[3 * t + k] : 3 * t + k;
56 u += mesh.texcoords[2 * corners[k]] / 3.0f;
57 v += mesh.texcoords[2 * corners[k] + 1] / 3.0f;
60 faces.push_back({.a = corner(corners[0]),
61 .b = corner(corners[1]),
62 .c = corner(corners[2]),
63 .color = ColorAt(texture, u, v)});
73 return MatrixMultiply(
74 MatrixMultiply(MatrixScale(size, size, size), MatrixRotateY(tree.
yaw)),
75 MatrixTranslate(tree.
x, terrain.
Height(tree.
x, tree.
z) - kSunk, tree.
z));
79 const Matrix& transform, Color tint) {
80 for (
const Face& face : faces) {
81 triangles.
Add(Vector3Transform(face.a, transform),
82 Vector3Transform(face.b, transform),
83 Vector3Transform(face.c, transform),
84 ColorTint(face.color, tint));
float Height(float x, float z) const
On the triangles between the grid's corners, as the client draws them.
void Add(Vector3 a, Vector3 b, Vector3 c, Color color)
std::vector< Face > LoadFaces(const std::string &path)
void AddFaces(Triangles &triangles, const std::vector< Face > &faces, const Matrix &transform, Color tint)
std::string TreesFolder()
Matrix TreeTransform(const Tree &tree, const Terrain &terrain)
Sizes the model, turns it, and stands it on the ground.
constexpr float kTreeHeight
In metres, before each tree's own scale.
float yaw
In radians, around the vertical.
float scale
How much bigger or smaller than the model it is.