nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
cube.cc
Go to the documentation of this file.
1#include "client/cube.h"
2
3#include <raylib.h>
4#include <raymath.h>
5
6#include <array>
7#include <cmath>
8#include <cstddef>
9#include <numbers>
10#include <utility>
11
12namespace nanommo {
13
14namespace {
15
16// Around the edge of a disc.
17constexpr int kSides = 24;
18
19// Where a spire's sides stop and its point starts, and how wide it is there,
20// from the middle, with the bottom half a unit wide.
21constexpr float kShoulder = 0.2f;
22constexpr float kNarrow = 0.38f;
23
24// Both shapes are convex, so a face points out when it points away from the
25// middle: a triangle that doesn't is turned over.
26void AddOutward(Triangles& triangles, Vector3 a, Vector3 b, Vector3 c,
27 Vector3 middle) {
28 const Vector3 centre =
29 Vector3Scale(Vector3Add(Vector3Add(a, b), c), 1.0f / 3);
30 if (Vector3DotProduct(FaceNormal(a, b, c), Vector3Subtract(centre, middle)) <
31 0.0f) {
32 std::swap(b, c);
33 }
34 triangles.Add(a, b, c, WHITE);
35}
36
37} // namespace
38
40 Triangles disc;
41 const Vector3 middle{0.0f, 0.0f, 0.0f};
42 const auto rim = [](int side, float y) {
43 const float angle = 2.0f * std::numbers::pi_v<float> * side / kSides;
44 return Vector3{0.5f * std::cos(angle), y, 0.5f * std::sin(angle)};
45 };
46 for (int side = 0; side < kSides; ++side) {
47 const Vector3 top = rim(side, 0.5f);
48 const Vector3 next_top = rim(side + 1, 0.5f);
49 const Vector3 bottom = rim(side, -0.5f);
50 const Vector3 next_bottom = rim(side + 1, -0.5f);
51 // From the top's middle, so that it points up, not out.
52 AddOutward(disc, {0.0f, 0.5f, 0.0f}, top, next_top, middle);
53 AddOutward(disc, bottom, next_bottom, next_top, middle);
54 AddOutward(disc, bottom, next_top, top, middle);
55 }
56 return disc;
57}
58
60 Triangles spire;
61 const Vector3 middle{0.0f, 0.0f, 0.0f};
62 // Its corners, around the vertical, at the bottom and at the shoulder.
63 constexpr std::array<Vector2, 4> kCorners = {
64 {{-1.0f, -1.0f}, {1.0f, -1.0f}, {1.0f, 1.0f}, {-1.0f, 1.0f}}};
65 const Vector3 point{0.0f, 0.5f, 0.0f};
66 for (std::size_t i = 0; i < kCorners.size(); ++i) {
67 const Vector2 here = kCorners[i];
68 const Vector2 next = kCorners[(i + 1) % kCorners.size()];
69 const Vector3 low{0.5f * here.x, -0.5f, 0.5f * here.y};
70 const Vector3 next_low{0.5f * next.x, -0.5f, 0.5f * next.y};
71 const Vector3 high{kNarrow * here.x, kShoulder, kNarrow * here.y};
72 const Vector3 next_high{kNarrow * next.x, kShoulder, kNarrow * next.y};
73 AddOutward(spire, low, next_low, next_high, middle);
74 AddOutward(spire, low, next_high, high, middle);
75 AddOutward(spire, high, next_high, point, middle);
76 }
77 return spire;
78}
79
80} // namespace nanommo
Definition chat.cc:9
Triangles Spire()
Definition cube.cc:59
Vector3 FaceNormal(Vector3 a, Vector3 b, Vector3 c)
Definition triangles.cc:22
Triangles Disc()
A disc a unit across and a unit tall, around its middle, with flat sides.
Definition cube.cc:39