nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
trees.cc
Go to the documentation of this file.
1#include "client/trees.h"
2
3#include <raymath.h>
4
5#include <algorithm>
6#include <string>
7#include <vector>
8
9namespace nanommo {
10
11namespace {
12
13// So that the trunk doesn't float over a slope.
14constexpr float kSunk = 0.15f;
15
16Color ColorAt(const Image& texture, float u, float v) {
17 const int x =
18 std::clamp(static_cast<int>(u * texture.width), 0, texture.width - 1);
19 const int y =
20 std::clamp(static_cast<int>(v * texture.height), 0, texture.height - 1);
21 return GetImageColor(texture, x, y);
22}
23
24} // namespace
25
26std::string TreesFolder() {
27 return std::string(GetApplicationDirectory()) + "trees";
28}
29
30std::vector<Face> LoadFaces(const std::string& path) {
31 if (!FileExists(path.c_str())) {
32 return {};
33 }
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]];
41 // raylib has uploaded the texture already, so we read it back.
42 Image texture =
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};
48 };
49 for (int t = 0; t < mesh.triangleCount; ++t) {
50 int corners[3];
51 float u = 0.0f;
52 float v = 0.0f;
53 for (int k = 0; k < 3; ++k) {
54 corners[k] = mesh.indices ? mesh.indices[3 * t + k] : 3 * t + k;
55 if (mesh.texcoords) {
56 u += mesh.texcoords[2 * corners[k]] / 3.0f;
57 v += mesh.texcoords[2 * corners[k] + 1] / 3.0f;
58 }
59 }
60 faces.push_back({.a = corner(corners[0]),
61 .b = corner(corners[1]),
62 .c = corner(corners[2]),
63 .color = ColorAt(texture, u, v)});
64 }
65 UnloadImage(texture);
66 }
67 UnloadModel(model);
68 return faces;
69}
70
71Matrix TreeTransform(const Tree& tree, const Terrain& terrain) {
72 const float size = kTreeHeight * tree.scale;
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));
76}
77
78void AddFaces(Triangles& triangles, const std::vector<Face>& faces,
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));
85 }
86}
87
88} // namespace nanommo
float Height(float x, float z) const
On the triangles between the grid's corners, as the client draws them.
Definition terrain.cc:102
void Add(Vector3 a, Vector3 b, Vector3 c, Color color)
Definition triangles.cc:27
Definition chat.cc:9
std::vector< Face > LoadFaces(const std::string &path)
Definition trees.cc:30
void AddFaces(Triangles &triangles, const std::vector< Face > &faces, const Matrix &transform, Color tint)
Definition trees.cc:78
std::string TreesFolder()
Definition trees.cc:26
Matrix TreeTransform(const Tree &tree, const Terrain &terrain)
Sizes the model, turns it, and stands it on the ground.
Definition trees.cc:71
constexpr float kTreeHeight
In metres, before each tree's own scale.
Definition trees.h:15
float yaw
In radians, around the vertical.
Definition terrain.h:38
float scale
How much bigger or smaller than the model it is.
Definition terrain.h:40