nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
chunks.cc
Go to the documentation of this file.
1#include "client/chunks.h"
2
3#include <raymath.h>
4
5#include <algorithm>
6#include <array>
7#include <vector>
8
9#include "client/ground.h"
10#include "client/triangles.h"
11
12namespace nanommo {
13
14std::vector<Chunk> LoadChunks(const Terrain& terrain, const Look& look,
15 const std::vector<Face>& tree, Shader shader) {
16 const int across = (terrain.cells() + kChunkCells - 1) / kChunkCells;
17 std::vector<Triangles> squares(across * across);
18 const Grid<Triangles> grid(squares.data(), across, across);
19 for (int j = 0; j < across; ++j) {
20 for (int i = 0; i < across; ++i) {
21 AddGround(grid[j, i], terrain, look, i * kChunkCells, j * kChunkCells,
23 }
24 }
25 // Each tree goes in with the ground under its trunk.
26 const float size = kChunkCells * Terrain::kCell;
27 for (const Tree& planted : terrain.trees()) {
28 const int i =
29 std::clamp(static_cast<int>((planted.x + terrain.half_size()) / size),
30 0, across - 1);
31 const int j =
32 std::clamp(static_cast<int>((planted.z + terrain.half_size()) / size),
33 0, across - 1);
34 AddFaces(grid[j, i], tree, TreeTransform(planted, terrain), look.trees);
35 }
36
37 std::vector<Chunk> chunks;
38 for (const Triangles& square : squares) {
39 const Mesh mesh = square.Upload();
40 Chunk chunk{.model = LoadModelFromMesh(mesh),
41 .box = GetMeshBoundingBox(mesh)};
42 chunk.model.materials[0].shader = shader;
43 chunks.push_back(chunk);
44 }
45 return chunks;
46}
47
48void UnloadChunks(std::vector<Chunk>& chunks) {
49 for (Chunk& chunk : chunks) {
50 UnloadModel(chunk.model);
51 }
52 chunks.clear();
53}
54
55bool InSight(const BoundingBox& box, Vector3 eye, const Matrix& view_projection,
56 float farthest) {
57 const Vector3 nearest = Vector3Clamp(eye, box.min, box.max);
58 if (Vector3Distance(eye, nearest) > farthest) {
59 return false;
60 }
61 // On the screen, a point's x, y and z are between -w and w. The box is out
62 // of sight if all eight of its corners are past the same one of those.
63 std::array<int, 6> past{};
64 for (int corner = 0; corner < 8; ++corner) {
65 const Vector3 point{corner & 1 ? box.max.x : box.min.x,
66 corner & 2 ? box.max.y : box.min.y,
67 corner & 4 ? box.max.z : box.min.z};
68 const Matrix& m = view_projection;
69 const std::array<float, 3> clip = {
70 m.m0 * point.x + m.m4 * point.y + m.m8 * point.z + m.m12,
71 m.m1 * point.x + m.m5 * point.y + m.m9 * point.z + m.m13,
72 m.m2 * point.x + m.m6 * point.y + m.m10 * point.z + m.m14};
73 const float w = m.m3 * point.x + m.m7 * point.y + m.m11 * point.z + m.m15;
74 for (int axis = 0; axis < 3; ++axis) {
75 past[2 * axis] += clip[axis] < -w;
76 past[2 * axis + 1] += clip[axis] > w;
77 }
78 }
79 return std::ranges::none_of(past, [](int corners) { return corners == 8; });
80}
81
82} // namespace nanommo
float half_size() const
From the middle to each side of the grid.
Definition terrain.h:88
static constexpr float kCell
In metres. Each cell of the grid is two triangles.
Definition terrain.h:48
const std::vector< Tree > & trees() const
Definition terrain.h:75
int cells() const
Along each side.
Definition terrain.h:85
Definition chat.cc:9
bool InSight(const BoundingBox &box, Vector3 eye, const Matrix &view_projection, float farthest)
Definition chunks.cc:55
std::vector< Chunk > LoadChunks(const Terrain &terrain, const Look &look, const std::vector< Face > &tree, Shader shader)
Definition chunks.cc:14
void AddGround(Triangles &triangles, const Terrain &terrain, const Look &look, int left, int top, int cells)
Definition ground.cc:69
constexpr int kChunkCells
In cells along each side: 32 metres.
Definition chunks.h:15
void AddFaces(Triangles &triangles, const std::vector< Face > &faces, const Matrix &transform, Color tint)
Definition trees.cc:78
void UnloadChunks(std::vector< Chunk > &chunks)
Definition chunks.cc:48
Matrix TreeTransform(const Tree &tree, const Terrain &terrain)
Sizes the model, turns it, and stands it on the ground.
Definition trees.cc:71
Model model
Definition chunks.h:20
A land's colours: its atmosphere, and the colours that go into its meshes.
Definition atmosphere.h:69
Color trees
Multiplies the colours of the trees' faces.
Definition atmosphere.h:77