nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
figures.cc
Go to the documentation of this file.
1#include "client/figures.h"
2
3#include <raymath.h>
4
5#include <algorithm>
6#include <array>
7#include <cmath>
8#include <numbers>
9#include <string>
10#include <utility>
11#include <vector>
12
13#include "client/triangles.h"
14
15namespace nanommo {
16
17namespace {
18
19constexpr float kPi = std::numbers::pi_v<float>;
20
21// Eight sides round each limb, and each end's cap is two rings of a ball: its
22// pole, and one this far round from it.
23constexpr int kSides = 8;
24constexpr float kCapAngle = 55.0f * DEG2RAD;
25
26template <typename T>
27T* Copy(const std::vector<T>& values) {
28 // UnloadMesh frees what's here, so raylib allocates it.
29 T* copy = static_cast<T*>(MemAlloc(values.size() * sizeof(T)));
30 std::ranges::copy(values, copy);
31 return copy;
32}
33
34// Triangles that share no corners, like the ground's, so each is lit flat,
35// with each corner bound to one bone. For the outline, each corner has the
36// shape's smooth normal instead.
37class Builder {
38 public:
39 explicit Builder(bool smooth) : smooth_(smooth) {}
40
41 // The shape's smooth normal at each corner says which side is outside, so
42 // the corners can come in either order.
43 void Add(Vector3 a, Vector3 b, Vector3 c, Vector3 na, Vector3 nb, Vector3 nc,
44 Color color, int bone) {
45 Vector3 face = FaceNormal(a, b, c);
46 if (Vector3DotProduct(face, Vector3Add(Vector3Add(na, nb), nc)) < 0.0f) {
47 std::swap(b, c);
48 std::swap(nb, nc);
49 face = Vector3Negate(face);
50 }
51 const std::array<std::pair<Vector3, Vector3>, 3> corners = {
52 {{a, na}, {b, nb}, {c, nc}}};
53 for (const auto& [corner, smooth] : corners) {
54 const Vector3 normal = smooth_ ? Vector3Normalize(smooth) : face;
55 positions_.insert(positions_.end(), {corner.x, corner.y, corner.z});
56 normals_.insert(normals_.end(), {normal.x, normal.y, normal.z});
57 colors_.insert(colors_.end(), {color.r, color.g, color.b, color.a});
58 // Up to four bones can move a corner, each by its weight, and each of
59 // ours moves with one, all the way.
60 bones_.insert(bones_.end(), {static_cast<unsigned char>(bone), 0, 0, 0});
61 weights_.insert(weights_.end(), {1.0f, 0.0f, 0.0f, 0.0f});
62 }
63 }
64
65 Mesh Upload() const {
66 Mesh mesh{};
67 mesh.vertexCount = static_cast<int>(positions_.size() / 3);
68 mesh.triangleCount = mesh.vertexCount / 3;
69 mesh.vertices = Copy(positions_);
70 mesh.normals = Copy(normals_);
71 mesh.colors = Copy(colors_);
72 mesh.boneIndices = Copy(bones_);
73 mesh.boneWeights = Copy(weights_);
74 UploadMesh(&mesh, false);
75 return mesh;
76 }
77
78 private:
79 bool smooth_;
80 std::vector<float> positions_;
81 std::vector<float> normals_;
82 std::vector<unsigned char> colors_;
83 std::vector<unsigned char> bones_;
84 std::vector<float> weights_;
85};
86
87// Two ways across the axis, square to it and to each other: the first across
88// the figure, or for a limb that goes across it, as the arms do, to the front.
89std::pair<Vector3, Vector3> Across(Vector3 axis) {
90 const Vector3 hint = std::fabs(axis.x) > 0.8f ? Vector3{0.0f, 0.0f, 1.0f}
91 : Vector3{1.0f, 0.0f, 0.0f};
92 const Vector3 u = Vector3Normalize(
93 Vector3Subtract(hint, Vector3Scale(axis, Vector3DotProduct(hint, axis))));
94 return {u, Vector3CrossProduct(axis, u)};
95}
96
97// A ring of corners round a limb: its middle, its radius, and how far its
98// normals lean from the axis, from none at a pole to a quarter turn round
99// the side.
100struct Ring {
101 Vector3 middle;
102 float radius;
103 float lean;
104};
105
106// Six rings from end to end: a cap at each end, of a pole and a ring part way
107// round, and between them, the limb's sides, from one radius to the other.
108void AddLimb(Builder& out, Vector3 a, Vector3 b, float ra, float rb,
109 float flatten, Color color, int bone) {
110 const Vector3 axis = Vector3Normalize(Vector3Subtract(b, a));
111 // Named, not bound, as clang-tidy's analyzer loses a structured binding's
112 // value in a lambda that captures it.
113 const std::pair<Vector3, Vector3> across = Across(axis);
114 const Vector3 u = across.first;
115 const Vector3 v = across.second;
116 const float c = std::cos(kCapAngle);
117 const float s = std::sin(kCapAngle);
118 const std::array<Ring, 6> rings = {{
119 {Vector3Subtract(a, Vector3Scale(axis, ra)), 0.0f, 0.0f},
120 {Vector3Subtract(a, Vector3Scale(axis, ra * c)), ra * s, kCapAngle},
121 {a, ra, kPi / 2.0f},
122 {b, rb, kPi / 2.0f},
123 {Vector3Add(b, Vector3Scale(axis, rb * c)), rb * s, kPi - kCapAngle},
124 {Vector3Add(b, Vector3Scale(axis, rb)), 0.0f, kPi},
125 }};
126 auto corner = [&](const Ring& ring, int i) {
127 const float turn = 2.0f * kPi * (i + 0.5f) / kSides;
128 const Vector3 out_u = Vector3Scale(u, std::cos(turn));
129 const Vector3 out_v = Vector3Scale(v, std::sin(turn));
130 const Vector3 at = Vector3Add(
131 ring.middle, Vector3Add(Vector3Scale(out_u, ring.radius),
132 Vector3Scale(out_v, ring.radius * flatten)));
133 const Vector3 around = Vector3Normalize(
134 Vector3Add(out_u, Vector3Scale(out_v, 1.0f / flatten)));
135 const Vector3 normal = Vector3Add(Vector3Scale(around, std::sin(ring.lean)),
136 Vector3Scale(axis, -std::cos(ring.lean)));
137 return std::pair{at, normal};
138 };
139 for (std::size_t r = 0; r + 1 < rings.size(); ++r) {
140 for (int i = 0; i < kSides; ++i) {
141 const auto [a0, n0] = corner(rings[r], i);
142 const auto [a1, n1] = corner(rings[r], (i + 1) % kSides);
143 const auto [b0, m0] = corner(rings[r + 1], i);
144 const auto [b1, m1] = corner(rings[r + 1], (i + 1) % kSides);
145 // At a pole, each ring's corners meet in one point, so its quads are
146 // triangles.
147 if (r == 0) {
148 out.Add(a0, b1, b0, n0, m1, m0, color, bone);
149 } else if (r + 2 == rings.size()) {
150 out.Add(a0, a1, b0, n0, n1, m0, color, bone);
151 } else {
152 out.Add(a0, a1, b1, n0, n1, m1, color, bone);
153 out.Add(a0, b1, b0, n0, m1, m0, color, bone);
154 }
155 }
156 }
157}
158
159// An icosahedron's twenty faces, each split into four, out to the radii: a
160// ball that's round, but low-poly, like the rest of the world.
161void AddBall(Builder& out, Vector3 middle, Vector3 radii, Color color,
162 int bone) {
163 const float t = (1.0f + std::sqrt(5.0f)) / 2.0f;
164 std::array<Vector3, 12> corners = {{{-1, t, 0},
165 {1, t, 0},
166 {-1, -t, 0},
167 {1, -t, 0},
168 {0, -1, t},
169 {0, 1, t},
170 {0, -1, -t},
171 {0, 1, -t},
172 {t, 0, -1},
173 {t, 0, 1},
174 {-t, 0, -1},
175 {-t, 0, 1}}};
176 for (Vector3& corner : corners) {
177 corner = Vector3Normalize(corner);
178 }
179 constexpr std::array<std::array<int, 3>, 20> kFaces = {
180 {{0, 11, 5}, {0, 5, 1}, {0, 1, 7}, {0, 7, 10}, {0, 10, 11},
181 {1, 5, 9}, {5, 11, 4}, {11, 10, 2}, {10, 7, 6}, {7, 1, 8},
182 {3, 9, 4}, {3, 4, 2}, {3, 2, 6}, {3, 6, 8}, {3, 8, 9},
183 {4, 9, 5}, {2, 4, 11}, {6, 2, 10}, {8, 6, 7}, {9, 8, 1}}};
184 auto place = [&](Vector3 p) {
185 return Vector3Add(middle, {p.x * radii.x, p.y * radii.y, p.z * radii.z});
186 };
187 // An egg's normals lean away from its long axis.
188 auto normal = [&](Vector3 p) {
189 return Vector3Normalize({p.x / radii.x, p.y / radii.y, p.z / radii.z});
190 };
191 auto add = [&](Vector3 a, Vector3 b, Vector3 c) {
192 out.Add(place(a), place(b), place(c), normal(a), normal(b), normal(c),
193 color, bone);
194 };
195 for (const auto& [i, j, k] : kFaces) {
196 const Vector3 a = corners[i];
197 const Vector3 b = corners[j];
198 const Vector3 c = corners[k];
199 const Vector3 ab = Vector3Normalize(Vector3Add(a, b));
200 const Vector3 bc = Vector3Normalize(Vector3Add(b, c));
201 const Vector3 ca = Vector3Normalize(Vector3Add(c, a));
202 add(a, ab, ca);
203 add(b, bc, ab);
204 add(c, ca, bc);
205 add(ab, bc, ca);
206 }
207}
208
209int BoneNamed(const Model& model, const std::string& name) {
210 for (int i = 0; i < static_cast<int>(model.skeleton.boneCount); ++i) {
211 if (name == model.skeleton.bones[i].name) {
212 return i;
213 }
214 }
215 return -1;
216}
217
218Vector3 Metres(const figures::Offset& offset, float metre) {
219 return {offset.x() * metre, offset.y() * metre, offset.z() * metre};
220}
221
222// The parts' triangles, with faces' normals, or for the outline, smooth ones.
223Mesh Build(const Model& model, const std::vector<figures::Part>& parts,
224 float metre, bool smooth) {
225 Builder out(smooth);
226 // The bind pose is where each bone rests, in the model's own space.
227 const Transform* rest = model.skeleton.bindPose;
228 for (const figures::Part& part : parts) {
229 const int bone = BoneNamed(model, part.bone());
230 const int from = part.from().empty() ? bone : BoneNamed(model, part.from());
231 const int to = BoneNamed(model, part.to());
232 if (bone < 0 || from < 0 || (smooth && part.flush())) {
233 continue;
234 }
235 const Color color = GetColor(part.color());
236 const Vector3 start =
237 Vector3Add(rest[from].translation, Metres(part.from_offset(), metre));
238 if (part.shape() == figures::Part::SHAPE_BALL) {
239 AddBall(out, start, Metres(part.size(), metre), color, bone);
240 } else if (part.shape() == figures::Part::SHAPE_LIMB && to >= 0) {
241 const Vector3 end =
242 Vector3Add(rest[to].translation, Metres(part.to_offset(), metre));
243 const float flatten = part.flatten() > 0.0f ? part.flatten() : 1.0f;
244 AddLimb(out, start, end, part.start_radius() * metre,
245 part.end_radius() * metre, flatten, color, bone);
246 }
247 }
248 return out.Upload();
249}
250
251} // namespace
252
253void Dress(Model& model, const std::vector<figures::Part>& parts, float metre) {
254 const Mesh mesh = Build(model, parts, metre, false);
255 for (int i = 0; i < model.meshCount; ++i) {
256 UnloadMesh(model.meshes[i]);
257 }
258 MemFree(model.meshes);
259 MemFree(model.meshMaterial);
260 model.meshCount = 1;
261 model.meshes = static_cast<Mesh*>(MemAlloc(sizeof(Mesh)));
262 model.meshes[0] = mesh;
263 model.meshMaterial = static_cast<int*>(MemAlloc(sizeof(int)));
264 // The parts' colours are their corners', so the material's is white.
265 model.materials[0].maps[MATERIAL_MAP_DIFFUSE].color = WHITE;
266}
267
268Mesh Parts(const Model& model, const std::vector<figures::Part>& parts,
269 float metre) {
270 return Build(model, parts, metre, false);
271}
272
273Mesh Hull(const Model& model, const std::vector<figures::Part>& parts,
274 float metre) {
275 return Build(model, parts, metre, true);
276}
277
278} // namespace nanommo
Definition chat.cc:9
void Dress(Model &model, const std::vector< figures::Part > &parts, float metre)
Definition figures.cc:253
Mesh Hull(const Model &model, const std::vector< figures::Part > &parts, float metre)
Definition figures.cc:273
Mesh Parts(const Model &model, const std::vector< figures::Part > &parts, float metre)
Definition figures.cc:268
Vector3 FaceNormal(Vector3 a, Vector3 b, Vector3 c)
Definition triangles.cc:22
A land, running: a World of it, with its NPCs in their cells.
Definition copies.h:20