nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
triangles.cc
Go to the documentation of this file.
1#include "client/triangles.h"
2
3#include <raymath.h>
4
5#include <algorithm>
6#include <vector>
7
8namespace nanommo {
9
10namespace {
11
12template <typename T>
13T* Copy(const std::vector<T>& values) {
14 // UnloadMesh frees what's here, so raylib allocates it.
15 T* copy = static_cast<T*>(MemAlloc(values.size() * sizeof(T)));
16 std::ranges::copy(values, copy);
17 return copy;
18}
19
20} // namespace
21
22Vector3 FaceNormal(Vector3 a, Vector3 b, Vector3 c) {
23 return Vector3Normalize(
24 Vector3CrossProduct(Vector3Subtract(b, a), Vector3Subtract(c, a)));
25}
26
27void Triangles::Add(Vector3 a, Vector3 b, Vector3 c, Color color) {
28 const Vector3 normal = FaceNormal(a, b, c);
29 for (const Vector3& corner : {a, b, c}) {
30 positions_.insert(positions_.end(), {corner.x, corner.y, corner.z});
31 normals_.insert(normals_.end(), {normal.x, normal.y, normal.z});
32 colors_.insert(colors_.end(), {color.r, color.g, color.b, color.a});
33 }
34}
35
36Mesh Triangles::Upload() const {
37 Mesh mesh{};
38 mesh.triangleCount = size();
39 mesh.vertexCount = 3 * size();
40 mesh.vertices = Copy(positions_);
41 mesh.normals = Copy(normals_);
42 mesh.colors = Copy(colors_);
43 UploadMesh(&mesh, false);
44 return mesh;
45}
46
47} // namespace nanommo
void Add(Vector3 a, Vector3 b, Vector3 c, Color color)
Definition triangles.cc:27
int size() const
Definition triangles.h:20
Mesh Upload() const
Hands them to the GPU, as a mesh for LoadModelFromMesh.
Definition triangles.cc:36
Definition chat.cc:9
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