nanoMMO 0.97.8Test coverage / Every file / client/trees.cc

Created: 2026-11-26 01:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/client/trees.cc
Line
Count
Source
1
#include "client/trees.h"
2
3
#include <raymath.h>
4
5
#include <algorithm>
6
#include <string>
7
#include <vector>
8
9
namespace nanommo {
10
11
namespace {
12
13
// So that the trunk doesn't float over a slope.
14
constexpr float kSunk = 0.15f;
15
16
1.72k
Color ColorAt(const Image& texture, float u, float v) {
17
1.72k
  const int x =
18
1.72k
      std::clamp(static_cast<int>(u * texture.width), 0, texture.width - 1);
19
1.72k
  const int y =
20
1.72k
      std::clamp(static_cast<int>(v * texture.height), 0, texture.height - 1);
21
1.72k
  return GetImageColor(texture, x, y);
22
1.72k
}
23
24
}  // namespace
25
26
11
std::string TreesFolder() {
27
11
  return std::string(GetApplicationDirectory()) + "trees";
28
11
}
29
30
11
std::vector<Face> LoadFaces(const std::string& path) {
31
11
  if (!FileExists(path.c_str())) {
32
0
    return {};
33
0
  }
34
11
  Model model = LoadModel(path.c_str());
35
11
  const BoundingBox box = GetModelBoundingBox(model);
36
11
  const float scale = 1.0f / (box.max.y - box.min.y);
37
11
  std::vector<Face> faces;
38
22
  for (int m = 0; m < model.meshCount; 
++m11
) {
39
11
    const Mesh& mesh = model.meshes[m];
40
11
    const Material& material = model.materials[model.meshMaterial[m]];
41
    // raylib has uploaded the texture already, so we read it back.
42
11
    Image texture =
43
11
        LoadImageFromTexture(material.maps[MATERIAL_MAP_DIFFUSE].texture);
44
5.18k
    const auto corner = [&](int i) {
45
5.18k
      return Vector3{mesh.vertices[3 * i] * scale,
46
5.18k
                     (mesh.vertices[3 * i + 1] - box.min.y) * scale,
47
5.18k
                     mesh.vertices[3 * i + 2] * scale};
48
5.18k
    };
49
1.73k
    for (int t = 0; t < mesh.triangleCount; 
++t1.72k
) {
50
1.72k
      int corners[3];
51
1.72k
      float u = 0.0f;
52
1.72k
      float v = 0.0f;
53
6.90k
      for (int k = 0; k < 3; 
++k5.18k
) {
54
5.18k
        corners[k] = mesh.indices ? mesh.indices[3 * t + k] : 
3 * t + k0
;
55
5.18k
        if (mesh.texcoords) {
56
5.18k
          u += mesh.texcoords[2 * corners[k]] / 3.0f;
57
5.18k
          v += mesh.texcoords[2 * corners[k] + 1] / 3.0f;
58
5.18k
        }
59
5.18k
      }
60
1.72k
      faces.push_back({.a = corner(corners[0]),
61
1.72k
                       .b = corner(corners[1]),
62
1.72k
                       .c = corner(corners[2]),
63
1.72k
                       .color = ColorAt(texture, u, v)});
64
1.72k
    }
65
11
    UnloadImage(texture);
66
11
  }
67
11
  UnloadModel(model);
68
11
  return faces;
69
11
}
70
71
3.03k
Matrix TreeTransform(const Tree& tree, const Terrain& terrain) {
72
3.03k
  const float size = kTreeHeight * tree.scale;
73
3.03k
  return MatrixMultiply(
74
3.03k
      MatrixMultiply(MatrixScale(size, size, size), MatrixRotateY(tree.yaw)),
75
3.03k
      MatrixTranslate(tree.x, terrain.Height(tree.x, tree.z) - kSunk, tree.z));
76
3.03k
}
77
78
void AddFaces(Triangles& triangles, const std::vector<Face>& faces,
79
3.03k
              const Matrix& transform, Color tint) {
80
476k
  for (const Face& face : faces) {
81
476k
    triangles.Add(Vector3Transform(face.a, transform),
82
476k
                  Vector3Transform(face.b, transform),
83
476k
                  Vector3Transform(face.c, transform),
84
476k
                  ColorTint(face.color, tint));
85
476k
  }
86
3.03k
}
87
88
}  // namespace nanommo