载入中...
搜索中...
未找到
HexMeshData.cpp
浏览该文件的文档.
2
3#include <cmath>
4
5namespace eve::hexmap {
6
7void HexMeshData::clear() noexcept {
8 positions_.clear();
9 normals_.clear();
10 uvs_.clear();
11 indices_.clear();
12}
13
14std::uint32_t HexMeshData::addVertex(HexVec3 position, float u, float v) {
15 const auto index = static_cast<std::uint32_t>(positions_.size() / 3u);
16 positions_.push_back(position.x);
17 positions_.push_back(position.y);
18 positions_.push_back(position.z);
19 uvs_.push_back(u);
20 uvs_.push_back(v);
21 return index;
22}
23
24void HexMeshData::addTriangle(std::uint32_t a, std::uint32_t b, std::uint32_t c) {
25 indices_.push_back(a);
26 indices_.push_back(b);
27 indices_.push_back(c);
28}
29
30void HexMeshData::addQuad(std::uint32_t a, std::uint32_t b, std::uint32_t c, std::uint32_t d) {
31 addTriangle(a, c, b);
32 addTriangle(b, c, d);
33}
34
35void HexMeshData::finalize() noexcept {
36 normals_.assign(positions_.size(), 0.f);
37 for (std::size_t t = 0; t + 2 < indices_.size(); t += 3) {
38 const std::uint32_t ia = indices_[t];
39 const std::uint32_t ib = indices_[t + 1];
40 const std::uint32_t ic = indices_[t + 2];
41 const auto at = [this](std::uint32_t i) {
42 const std::size_t base = static_cast<std::size_t>(i) * 3u;
43 return HexVec3{positions_[base], positions_[base + 1], positions_[base + 2]};
44 };
45 const HexVec3 a = at(ia);
46 const HexVec3 b = at(ib);
47 const HexVec3 c = at(ic);
48 const HexVec3 ab = b - a;
49 const HexVec3 ac = c - a;
50 HexVec3 n{ab.y * ac.z - ab.z * ac.y, ab.z * ac.x - ab.x * ac.z, ab.x * ac.y - ab.y * ac.x};
51 const float len = std::sqrt(n.x * n.x + n.y * n.y + n.z * n.z);
52 if (len > 1e-8f) {
53 n.x /= len;
54 n.y /= len;
55 n.z /= len;
56 } else {
57 n = HexVec3{0.f, 1.f, 0.f};
58 }
59 for (std::uint32_t i : {ia, ib, ic}) {
60 const std::size_t base = static_cast<std::size_t>(i) * 3u;
61 normals_[base] = n.x;
62 normals_[base + 1] = n.y;
63 normals_[base + 2] = n.z;
64 }
65 }
66}
67
68} // namespace eve::hexmap
float u
Definition Grass.cpp:233
glm::vec3 n
Definition Grass.cpp:63
std::uint32_t ab
std::uint32_t ac
CPU vertex/index container shared by the hex surface builders.
float v
std::int32_t c
std::array< float, 3 > position
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
float d
float t
uint32_t index
std::size_t at
void finalize() noexcept
Computes flat per-face normals. Must be called before upload.
void addTriangle(std::uint32_t a, std::uint32_t b, std::uint32_t c)
Appends a triangle with counter-clockwise winding.
void addQuad(std::uint32_t a, std::uint32_t b, std::uint32_t c, std::uint32_t d)
Appends a quad as two triangles (a,c,b and b,c,d).
void clear() noexcept
Removes every vertex and index, keeping the allocated capacity.
std::uint32_t addVertex(HexVec3 position, float u, float v)
Appends one vertex.
Minimal 3-component float vector used by the hex mesh builders.
Definition HexMetrics.h:18