-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMesh.cpp
More file actions
135 lines (113 loc) · 3.07 KB
/
Copy pathMesh.cpp
File metadata and controls
135 lines (113 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "Mesh.h"
#include "VertexBufferFactory.h"
#include "Core/Logger.h"
namespace sh::render
{
Mesh::Mesh() :
topology(Topology::Face)
{
}
Mesh::Mesh(const Mesh& other) :
verts(other.verts), indices(other.indices), faces(other.faces),
topology(other.topology),
bounding(other.bounding)
{
buffer = other.buffer->Clone();
}
Mesh::Mesh(Mesh&& other) noexcept :
verts(std::move(other.verts)), indices(std::move(other.indices)), faces(std::move(other.faces)),
buffer(std::move(other.buffer)),
topology(other.topology),
bounding(std::move(other.bounding))
{
}
Mesh::~Mesh()
{
SH_INFO("~Mesh");
}
SH_RENDER_API auto Mesh::operator=(const Mesh& other) -> Mesh&
{
verts = other.verts;
indices = other.indices;
faces = other.faces;
buffer = other.buffer->Clone();
topology = other.topology;
bounding = other.bounding;
return *this;
}
SH_RENDER_API auto Mesh::operator=(Mesh&& other) noexcept -> Mesh&
{
indices = std::move(other.indices);
faces = std::move(other.faces);
verts = std::move(other.verts);
buffer = std::move(other.buffer);
topology = other.topology;
bounding = std::move(other.bounding);
return *this;
}
SH_RENDER_API void Mesh::SetVertex(const std::initializer_list<Vertex>& verts)
{
this->verts.clear();
this->verts.reserve(verts.size());
for (auto& vert : verts)
this->verts.push_back(vert);
}
SH_RENDER_API void Mesh::SetIndices(const std::initializer_list<uint32_t>& indices)
{
this->indices.resize(indices.size());
for (int i = 0; i < indices.size(); ++i)
{
this->indices[i] = *(indices.begin() + i);
}
}
SH_RENDER_API void Mesh::Build(const IRenderContext& context)
{
CreateFace();
buffer = VertexBufferFactory::Create(context, *this);
}
SH_RENDER_API void Mesh::CalculateTangents()
{
for (size_t i = 0; i < indices.size(); i += 3)
{
uint32_t i0 = indices[i];
uint32_t i1 = indices[i + 1];
uint32_t i2 = indices[i + 2];
Vertex& v0 = verts[i0];
Vertex& v1 = verts[i1];
Vertex& v2 = verts[i2];
glm::vec3 edge1 = v1.vertex - v0.vertex;
glm::vec3 edge2 = v2.vertex - v0.vertex;
glm::vec2 deltaUV1 = v1.uv - v0.uv;
glm::vec2 deltaUV2 = v2.uv - v0.uv;
float f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y + 1e-8f);
glm::vec3 tangent;
tangent.x = f * (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x);
tangent.y = f * (deltaUV2.y * edge1.y - deltaUV1.y * edge2.y);
tangent.z = f * (deltaUV2.y * edge1.z - deltaUV1.y * edge2.z);
tangent = glm::normalize(tangent);
v0.tangent += tangent;
v1.tangent += tangent;
v2.tangent += tangent;
}
for (auto& v : verts)
v.tangent = glm::normalize(v.tangent);
}
SH_RENDER_API void Mesh::SetVertexBuffer(std::unique_ptr<IVertexBuffer> buf)
{
buffer = std::move(buf);
}
SH_RENDER_API void Mesh::CreateFace()
{
if (topology == Topology::Face)
{
for (int i = 0; i < indices.size(); i += 3)
{
Face face{};
face.vertexIdx[0] = indices[i + 0];
face.vertexIdx[1] = indices[i + 1];
face.vertexIdx[2] = indices[i + 2];
faces.push_back(face);
}
}
}
}//namespace